NameError: name 'Right' is not defined

NameError: name 'Right' is not defined

我正在尝试使用 Python 2.7.15 在 Spyder 3.3.1 中执行以下代码。我是初学者。

  text = str(input("You are lost in forest..."))
  while text == "Right":
      text = str(input("You are lost in forest..."))
  print "You got out of the forest!!!"

当我 运行 具有整数值的代码时,它起作用了。例如下面的一段代码:

  text = input("You are lost in forest...")
  while text == 1:
      text = input("You are lost in forest...")
  print "You got out of the forest!!!"

如何使 input() 使用字符串值?感谢您的帮助。

使用raw_input()代替input():

value = raw_input("You are lost in forest...") # String input 
value = int(raw_input("You are lost in forest...")) # Integer input 
...

在 python 2 中,raw_input() 准确获取用户键入的内容并将其作为字符串传回。 input() 试图理解 user.Hence 输入的数据期望语法正确 python statement.That 就是为什么当您输入 Right 时出现错误。因此,您可以输入 "Right" 作为输入来修复此错误。

但是最好用raw_input()代替input()