Input()无法为名称赋值



我正在构建一个相当大的函数,该函数一开始就需要使用pickinput进行用户输入。我在python终端中使用python 3.8。

我正在使用last_6 = input("What is your average milage for the last 6 weeks?)为名称last_6分配用户输入值。当我调用函数时,会要求我输入值,但函数无法进入下一步。

调用现在应该返回输入值last_6的名称会导致错误消息

NameError: name 'last_6' not is not defined .

功能的第一部分,直到错误发生时,可以在这里看到:

def milage_calc():
title = 'What event are you training for?'
options = [ '800', '1500/mile','3k/2mile', '5k', '10k', 'Half Marathon', 'Marathon' ]
event = pick(options, title, multiselect= False)
event_dict = {'800': 0.7, '1500/mile': 0.75,'3k/2mile': 0.8, '5k': 0.9, '10k': 1 , 'Half Marathon': 1.1, 'Marathon': 1.2, }
event_modifier = event_dict[event[0]]
weeks = list(range(1,20,1))
_20_weeks_milage = {}

last_6 = input("what is your average weekly milage over the last 6 weeks? Please give your answer as a whole number:")

终端显示what is your average weekly milage over the last 6 weeks? Please give your answer as a whole number:

然后我输入答案,然后点击回车键。但是函数的其余部分不会运行。当我调用与输入值last_6相关联的名称时,我只得到NameError: name 'last_6' is not defined

完整的功能可以在这里看到:

def milage_calc() :
title = 'What event are you training for?'
options = [ '800', '1500/mile','3k/2mile', '5k', '10k', 'Half Marathon', 'Marathon' ]
event = pick(options, title, multiselect= False)
event_dict = {'800': 0.7, '1500/mile': 0.75,'3k/2mile': 0.8, '5k': 0.9, '10k': 1 , 'Half Marathon': 1.1, 'Marathon': 1.2, }
event_modifier = event_dict[event[0]]
weeks = list(range(1,20,1))
_20_weeks_milage = {}

last_6 = input("what is your average weekly milage over the last 6 weeks? Please give your answer as a whole number:")
# check that milage input is in the right format
while type(last_6) != int:
print("You didn't input your milage as a whole number, please try again.")
last_6 = int(input("what is your average weekly milage over the last 6 weeks? Please give your answer as a whole number:" ))
if 0 > last_6 or 100 < last_6:
print("Please re-enter your milage to confirm that it is correct")
last_6_2 = input("what is your average weekly milage over the last 6 weeks? Please give your answer as a whole number:")
if last_6_2 == last_6:
None
else:
print("Your entries did not match. Please re-enter your milage to confirm that it is correct. Becareful because your training plan will n be created using the milage you enter this time")
last_6 = input("what is your average weekly milage over the last 6 weeks? Please give your answer as a whole number:")
# User inputs the ave. milage per week for their biggest 6 weeks of the last year
best_6 = input("In the last year, what is your highest 6 week average milage? Please give your answer as a whole number:")
# check that milage input is in the right format
while type(best_6) != int:
print("You didn't input your milage as a whole number, please try again.")
best_6 = input("In the last year, what is your highest 6 week average milage? Please give your answer as a whole number:")
if 0 > best_6 or 100 < best_6:
print("Please re-enter your milage to confirm that it is correct")
best_6_2 = input("In the last year, what is your highest 6 week average milage? Please give your answer as a whole number:")
if best_6_2 == best_6:
None
else:
print("Your entries did not match. Please re-enter your milage to confirm that it is correct. Becareful because your training plan will n be created using the milage you enter this time")
best_6 = input("what is your average weekly milage over the last 6 weeks? Please give your answer as a whole number:")
# Calculating the milage for each week of the 20 weeks of the training plan.
for x in weeks:
if x in [1,2,3]:
_20_weeks_milage[x] = 10 + 0.9*((last_6)+x*0.2*(best_6 - last_6))
elif x in [4,8,12,16]:
_20_weeks_milage[x]= 0.75*_20_weeks_milage[(x-1)]
elif x in [5,6,7,9,10,11,13]:
_20_weeks_milage[x]= best_6 + 3*(x-5)
elif x in [14,15,17,18]:
_20_weeks_milage[x]= (_20_weeks_milage[13]-((1.5*x)-13))*event_modifier
else :
_20_weeks_milage[x]= (_20_weeks_milage[16]*(0.50+((20-x)*0.125)))*event_modifier
return print(_20_weeks_milage)
正如Frank所评论的,input()总是返回一个字符串,这会导致循环失败。要检查输入是否可以安全地转换为整数,可以使用.isdigit()方法。然而,这只适用于整数。以下是您的代码的第一个循环,并牢记这一点:
last_6 = input("what is your average weekly milage over the last 6 weeks? " 
"Please give your answer as a whole number:")
# check that milage input is in the right format
while not last_6.isdigit():
print("You didn't input your milage as a whole number, please try again.")
last_6 = input("what is your average weekly milage over the last 6 weeks? "
"Please give your answer as a whole number:")
last_6 = int(last_6)

此外,你发布的代码格式不正确,我不得不猜测一些缩进。。。

最新更新