python-2.7 - 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() 与字符串值一起使用?谢谢您的帮助。
解决方案
使用raw_input()
代替input()
:
value = raw_input("You are lost in forest...") # String input
value = int(raw_input("You are lost in forest...")) # Integer input
...
推荐阅读
- selenium - 除非我手动单击浏览器元素,否则 Chromedriver 不起作用
- javascript - 将多个纹理应用于 javascript 画布中的不同图像
- typescript - 如何开玩笑地模拟对象
- get - 响应无法序列化,输入数据为零或零长度
- python - How to draw a square in an image from its center (x,y) in opencv Python?
- spring-boot - 在 Spring Boot+JMS+IBM MQ Durable Topic 监听器中设置 brokerDurSubQueue 属性
- r - R:在 R 中创建递归二叉树
- javascript - 我正在尝试使用一个按钮将背景颜色恢复为加载页面时的渐变色
- outlook - :iCal 事件未添加到组织者日历中
- mule - 在 Mule 中管理和操作大型 sql 查询的最佳方法