【问题标题】:Input() failing to assign a value to a nameInput() 未能为名称赋值
【发布时间】:2020-10-30 16:10:40
【问题描述】:

我正在构建一个相当大的函数,它需要用户在开始时使用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)

【问题讨论】:

  • 请注意正确格式化您的代码!如果我们必须从格式错误中分辨出实际情况,那么调试代码是非常困难的。您可以edit您的问题来修复格式。另请查看minimal reproducible example 帮助页面。
  • 不是您问题的答案,但您的第一个 last_6 = input(...) 必须是 last_6 = int(input(...))。否则,结果为字符串,而您的 type(last_6) != int 始终为 true。
  • 你需要在@Yellin 函数正确之前定义 last_6
  • 您是否尝试从 python 终端访问last_6

标签: python input nameerror


【解决方案1】:

正如弗兰克所说,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)

另外,您发布的代码格式不正确,我不得不猜测一些缩进...

【讨论】:

  • 这与该问题实际询问的所谓的NameError: name 'last_6' not is not defined 有什么关系?
  • 我不知道错误是从哪里来的,实际上 - 查看代码,函数只是卡在last_6_2best_6 的输入处。也许他们试图从函数外部访问last_6
  • 我也不知道错误可能来自哪里,但这就是问题所要问的。
猜你喜欢
  • 1970-01-01
  • 2014-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-31
  • 2021-07-05
  • 1970-01-01
  • 2022-12-03
相关资源
最近更新 更多