2016-11-09 2 views
-1

Ich habe versucht, einer Variablen wie Gesundheit oder Glück eine Zufallszahl zuzuweisen, aber es scheint nie zu funktionieren, als wenn ich die Variable drucke, sagt es einfach, dass es nicht definiert ist. Ich wäre sehr dankbar für jede Hilfe mit diesem. Vielen Dank.Wie könnte ich einer Variablen eine zufällige Ganzzahl zuweisen?

from random import randint 

def ageroll(): 
    age= int(input("How old will you be?")) 
    if age <= 0: 
     print("Input a valid number from 1 to 120") 
     ageroll() 
    elif age <= 18: 
     health = randint(1,4) 
     agility = randint(4,8) 
     strength = randint(1,2) 
     luck = randint(1,10) 
     endurance = randint(2,4) 
     intelligence = randint(1,5) 
     charm = randint(1,7) 
     return(health,agility,strength,luck,endurance,intelligence,charm) 
    elif age <= 35: 
     health = randint(4,10) 
     agility = randint(5,9) 
     strength = randint(5,10) 
     luck = randint(1,10) 
     endurance = randint(4,8) 
     intelligence = randint(3,10) 
     charm = randint(4,7) 
    elif age <= 56: 
     health = randint(4,8) 
     agility = randint(3,6) 
     strength = randint(3,10) 
     luck = randint(1,10) 
     endurance = randint(5,8) 
     intelligence = randint(5,10) 
     charm = randint(6,10) 
    elif age <= 78: 
     health = randint(2,5) 
     agility = randint(2,5) 
     strength = randint(2,6) 
     luck = randint(1,10) 
     endurance = randint(1,4) 
     intelligence = randint(6,10) 
     charm = randint(5,6) 
    elif age <= 101: 
     health = randint(1,5) 
     agility = randint(1,4) 
     strength = randint(1,4) 
     luck = randint(4,10) 
     endurance = randint(3,9) 
     intelligence = randint(3,10) 
     charm = randint(4,7) 
    elif age <= 120: 
     health = randint(1,2) 
     agility = randint(1,3) 
     strength = randint(1,4) 
     luck = 10 
     endurance = randint(6,8) 
     intelligence = randint(10,10) 
     charm = randint(6,10) 
    else: 
     print("Input a valid number from 1 to 120") 
     ageroll() 
+4

Sie nur etwas zurück, wenn 'age' <= 18. – Daniel

+0

Willkommen bei Paketüberfluss. Bitte lesen und befolgen Sie die Buchungsrichtlinien in der Hilfe. [Minimales, vollständiges, überprüfbares Beispiel] (http://stackoverflow.com/help/mcve) gilt hier. Wir können Ihnen nicht effektiv helfen, bis Sie Ihren Code veröffentlicht und das Problem genau beschrieben haben. Insbesondere löst der von Ihnen gepostete Code das von Ihnen beschriebene Problem nicht aus. – Prune

+0

Ich würde eine Druckanweisung unter den Optionen hinzufügen, um zu sehen, ob sie eine davon auswählen. h., "elif age <= 56", print "age is", Alter, um zu sehen, dass es in den elif-Block eingetreten ist. @Daniel hat Recht, du kommst nur von einem der Blöcke zurück. Diese Variablen bleiben lokal ... Sie werden am Ende Ihres if/else Blocks zurückkehren wollen. – mauve

Antwort

0

Sie haben Ihre Variablen zurückzukehren, und weisen Sie diesen Rückgabewert wieder auf eine Variable, es zu benutzen:

from random import randint 

PROPERTY_RANGES = [ 
    (18, dict(health=(1,4), agility=(4,8), strength=(1,2), luck=(1,10), endurance=(2,4), intelligence=(1,5), charm=(1,7))), 
    (35, dict(health=(4,10), agility=(5,9), strength=(5,10), luck=(1,10), endurance=(4,8), intelligence=(3,10), charm=(4,7))), 
    (56, dict(health=(4,8), agility=(3,6), strength=(3,10), luck=(1,10), endurance=(5,8), intelligence=(5,10), charm=(6,10))), 
    (78, dict(health=(2,5), agility=(2,5), strength=(2,6), luck=(1,10), endurance=(1,4), intelligence=(6,10), charm=(5,6))), 
    (101, dict(health=(1,5), agility=(1,4), strength=(1,4), luck=(4,10), endurance=(3,9), intelligence=(3,10), charm=(4,7))), 
    (120, dict(health=(1,2), agility=(1,3), strength=(1,4), luck=(10,10), endurance=(6,8), intelligence=(10,10), charm=(6,10))), 
] 

def ageroll(): 
    while True: 
     age = int(input("How old will you be?")) 
     if 0 < age <= 120: 
      break 
     print("Input a valid number from 1 to 120") 
    for max_age, values in PROPERTY_RANGES: 
     if age <= max_age: 
      break 
    return {k: randint(*r) for k, r in values.items()} 

properties = ageroll() 
print(properties['health']) 
Verwandte Themen