2016-11-09 81 views
-1

我一直在試圖給一個變量賦予一個隨機數,比如健康或運氣,但它似乎並沒有像我打印變量時那樣工作,它只是說它沒有被定義。我將不勝感激任何幫助。非常感謝你。我怎樣才能將一個隨機整數賦值給一個變量?

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

你只能回到什麼時候'age' <= 18 – Daniel

+0

歡迎堆棧溢出。請閱讀並遵守幫助文檔中的發佈準則。 [最小,完整,可驗證的示例](http://stackoverflow.com/help/mcve)適用於此處。在您發佈代碼並準確描述問題之前,我們無法有效幫助您。具體而言,您發佈的代碼不會引發您描述的問題。 – Prune

+0

我會在選項下添加一個打印語句,以查看它是否選擇了它們中的任何一個。即「elif年齡<= 56」,印刷「年齡是」,年齡看它進入elif塊。 @丹尼爾是對的,你只是從一個街區回來。這些變量保持本地化......您將要在if/else塊的末尾返回 – mauve

回答

0

你必須返回你的變量,並重新分配這個返回值的變量,使用它:

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']) 
相關問題