python基础问题求解-关于循环

plutoinkuiper 2016-12-18 03:17:02
求教,菜鸟一枚
正在学习循环语句,照着书上敲击的代码
while True:
print 'Who are you?'
name = input()
if name != 'Joe':
continue
print 'Hello, Joe. What is the password?(It is a fish.) '
password = input()
if password == 'swordfish':
break
print 'Access granted.'

运行结果是:
Who are you?
Joe
Traceback (most recent call last):
File "C:/Users/ranzh/Desktop/ceshi.py", line 4, in <module>
name = input()
File "<string>", line 1, in <module>
NameError: name 'Joe' is not defined

不解,求赐教。书上的代码里面,python是3.x版本,我是2.7,所以自己敲的代码里print没有加()

谢谢!
...全文
255 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
广印大叔 2016-12-20
  • 打赏
  • 举报
回复
while True: print 'Who are you?' name = raw_input() if name != 'Joe': continue print 'Hello, Joe. What is the password?(It is a fish.) ' password = raw_input() if password == 'swordfish': break This is OK. as xpresslink said
vfx_dli 2016-12-19
  • 打赏
  • 举报
回复
第四行字符串左右符号不一样
xiaomXxXx 2016-12-19
  • 打赏
  • 举报
回复
python 3.x版本应该是不区分input()和raw_inout()了,你检查一下第四行joe两边的单引号的格式是否正确。
混沌鳄鱼 2016-12-18
  • 打赏
  • 举报
回复
把 input() 改成 raw_input()就可以了。
plutoinkuiper 2016-12-18
  • 打赏
  • 举报
回复

37,719

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧