首页 > 解决方案 > NameError:未定义名称“Right”

问题描述

我正在尝试使用 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() 与字符串值一起使用?谢谢您的帮助。

标签: python-2.7nameerror

解决方案


使用raw_input()代替input()

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

推荐阅读