通过艰苦的方式学习python练习14不要在命令提示符/powershell中运行



我正在艰难地学习python,练习14让我制作一个脚本,每当我尝试执行脚本时,无论出于什么原因,都会提示用户一些不同的问题(通常是在cmpt中键入"python ex14 username")这是的代码

from sys import argv 
script, user_name = argv 
prompt = '>' 
print " Hi %s, I'm the %s script. " % (user_name,script) 
print "I'd like to ask you a few questions ." 
print "Do you like me %s?" % user_name 
lives = raw_input(prompt) 
print "where do you live %s?" % user_name 
lives = raw_input(prompt)  
print "What kind of computer do you have?" 
computer = raw_input(prompt)                                  
print """
Right, so you said %r about liking me. 
You live in %r. Not sure where that is 
And you have a %r computer. Nice. 
""" % (likes, lives, computer)  

此外,我正在使用Windows 7和Notepad++

脚本本身没有任何问题,只是您的第一个raw_input变量是"lifes",而它应该是"likes"。

在我看来,如果您试图通过键入python ex14 username来运行脚本,那么问题很可能只是您忘记键入.py扩展名。只需键入python ex14.py username,并确保您位于存储python脚本的目录中,那么您就应该一切正常。

如果这不是问题所在,那么你应该键入你收到的确切错误信息,这样人们就能得到更多帮助。编码快乐!

它工作正常,你只是在第9行打错了字,它应该是"喜欢"而不是"生活"

Microsoft Windows [Version 6.2.9200]
(c) 2012 Microsoft Corporation. All rights reserved.
E:>python ex14.py yanuar
 Hi yanuar, I'm the ex14.py script.
I'd like to ask you a few questions .
Do you like me yanuar?
>yes
where do you live yanuar?
>ina
What kind of computer do you have?
>vaio
Traceback (most recent call last):
  File "ex14.py", line 21, in <module>
    """% (likes, lives, computer)
NameError: name 'likes' is not defined

最后一个脚本是打印所有变量,以及它所期望的"赞"变量。

E:>python ex14.py yanuar
 Hi yanuar, I'm the ex14.py script.
I'd like to ask you a few questions .
Do you like me yanuar?
>Yes
where do you live yanuar?
>INA
What kind of computer do you have?
>Vaio
Right, so you said 'Yes' about liking me.
You live in 'INA'. Not sure where that is
And you have a 'Vaio' computer. Nice.

最新更新