【问题标题】:Whats wrong with this python code? I get an error after I input这个python代码有什么问题?输入后出现错误
【发布时间】:2011-12-28 04:29:30
【问题描述】:
while print('Type ROLL to roll for your stats.'):
    roll = input() # I get my error right after I input
    if roll == "roll" or roll == "ROLL":
        strength = random.randint(1, 100)
        defense = random.randint(1, 100)
        attack = random.randint(1, 100)
    print('Your attack level is ' + attack + '.')
    print('Your strength level is ' + strength + '.')
    print('Your defense level is ' + defense + '.')
    print('Would you like to reroll?')
    reroll = input()
    if reroll == no or reroll == NO:
        break

我的错误是

Traceback (most recent call last):
roll
NameError: name 'roll' is not defined

【问题讨论】:

  • 您使用的是 Python 2.x 还是 Python 3.x? input()函数在每个版本中是不同的。
  • if reroll == no or reroll == NO: - 那是什么?
  • @paxdiablo - that 是这个 OP 提出的下一个 SO 问题。
  • 您确定在运行之前保存了代码吗?你是如何运行代码的?你输入了什么?

标签: python string variables


【解决方案1】:

这里和那里的一些事情:

  • 您的 while 循环将不起作用,因为 print 的返回值是 None。它不会执行,我认为这不是预期的。您可以通过while Truewhile 1 将其更改为无限循环。理论上,当有人输入“否”时,您的代码跳出循环,但我们会解决这个问题...

  • 您可以更改您的 input() 语句以包含您希望在终端中显示的文本,然后再输入它们。例如,您可以将其中一个更改为roll = input('Type ROLL to roll for your stats.)

  • noNO 均未定义为变量。请记住,它们必须是 strings 才能被评估,因为这正是您要寻找的。将来,查看if reroll.lower() == 'no' 之类的语句可能会更有优势,因为这样可以避免重复键入多个字符串条目值。

希望这些能让您走上正轨。不要忘记,如果您在此处看到喜欢的答案,请随时接受(这样社区就知道您已经得到了答案)。

【讨论】:

    【解决方案2】:

    由于您似乎使用的是 Python 2.x,因此请使用 raw_input 而不是 input。您可能正在学习专为 Python 3.x 设计的教程。

    【讨论】:

    • 嗯,您描述的行为与 Python 2.x 一致。如果您改为尝试raw_input,会发生什么?
    • 对不起,没有。 raw_input不会 导致与您显示的相同的问题,无论您是在 Python 2.x 还是 Python 3.x 中(在 Python 2.x 和 不同的 Python 3.x 中的错误,因为raw_input 在 Python 3.x 中不存在)。确保您实际运行的是您认为正在运行的修改后的代码。
    • raw_input 只是从标准输入中读取一个字符串。所以,正如@GregHewgill 所说,你不可能得到同样的错误
    【解决方案3】:

    也许:

    roll = eval(input())

    import sys roll = sys.stdin.readline()

    根据: http://docs.python.org/release/3.0.1/whatsnew/3.0.html

    http://www.python.org/dev/peps/pep-3111/

    【讨论】:

    • Brian,eval(input()) 是一种让 Python3 输入像 Python2 一样工作的方法。我不确定它是否与此处相关。
    • 你是对的,那个 eval 没有任何意义。感谢您的洞察力,我正在完成 2/3 过渡...
    【解决方案4】:

    在 Python 2 中,使用 input 命令会导致您描述的错误:

    >>> blah = input()
    roll
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<string>", line 1, in <module>
    NameError: name 'roll' is not defined
    

    对于 Python 2,您可以使用 raw_input:

    >>> blah = raw_input()
    roll
    >>> print blah
    roll
    

    在 Python 3 中,第一个示例运行良好 - raw_input 被重命名为 input - 也许你不小心运行了错误的命令?您可以通过以下方式验证这一点:

    import sys
    print sys.version
    

    ..在你的脚本某处

    【讨论】:

      【解决方案5】:
      roll = input('Type ROLL to roll for your stats. ')
      if roll == "roll" or roll == "ROLL":
          #do your stats roll here
      reroll = input('Would you like to reroll? ')
      if reroll == 'no' or reroll == 'NO':
          break
      

      我相信您的错误很可能来自您正在打印问题,然后传递了一个空白的 input() 函数。当您使用输入功能时,您应该在括号内输入您的问题作为字符串。

      input('Place your question / prompt here, with a space at the end so there is a gap between the question and your input: ')
      

      另外,当你测试 reroll 是否等于 no 或 NO 时,你必须记住 no 和 NO 必须用引号括起来表示一个字符串! Python 将不在引号中的任何内容视为数字、变量、函数或保留字(如 OR)。如果您尚未将 no 和 NO 声明为变量,则您的程序将无法运行。因此,您必须在 no 和 NO 周围加上单引号或双引号,或者在程序开始时必须将它们声明为变量,例如

      no = 'no'
      NO = 'NO'
      

      另外,while 循环被破坏了。对于无限循环,将第一条语句设置为While True。如果您希望循环在满足某个条件时中断,请在循环开始之前声明另一个变量并将其值设置为True(布尔值 true,而不是单词 true)。完成后,您可以将该变量设置为布尔值 False 以退出循环

      rolling = True
      While rolling:
          #do stuff
      If exitcondition == True:
          rolling = False # When exitcondition is True, rolling is False and the loop is broken
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-02
        相关资源
        最近更新 更多