【发布时间】:2020-10-30 16:10:40
【问题描述】:
我正在构建一个相当大的函数,它需要用户在开始时使用pick 和input 输入。我正在 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)
【问题讨论】:
-
请注意正确格式化您的代码!如果我们必须从格式错误中分辨出实际情况,那么调试代码是非常困难的。您可以edit您的问题来修复格式。另请查看minimal reproducible example 帮助页面。
-
不是您问题的答案,但您的第一个
last_6 = input(...)必须是last_6 = int(input(...))。否则,结果为字符串,而您的type(last_6) != int始终为 true。 -
你需要在@Yellin 函数正确之前定义 last_6
-
您是否尝试从 python 终端访问
last_6?