简体   繁体   English

学习python练习14不在命令提示符/ powershell中运行的困难方式

[英]learn python the hard way exercise 14 not running in command prompt/powershell

I'm doing learn python the hard way and exercise 14 makes me make a script which prompts the user a few different questions and for whatever reason whenever I try and execute the script (normally by typing in "python ex14 username" in cmpt) Here is the code for it 我正在努力学习python,练习14使我编写了一个脚本,该脚本在每次尝试执行该脚本时都会提示用户一些不同的问题,无论出于何种原因(通常通过在cmpt中输入“ python ex14用户名”)是它的代码

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)  

Also I'm using Windows 7 and Notepad++ 我也在使用Windows 7和Notepad ++

There is nothing wrong with the script itself, except your first raw_input variable is 'lives' when it should be 'likes'. 脚本本身没有什么问题,除了您的第一个raw_input变量应为“ raw_input ”时为“生命”。

It seems to me that if you're trying to run the script by typing python ex14 username then the problem is most likely simply that you forgot to type in the .py extension. 在我看来,如果您尝试通过键入python ex14 username来运行脚本,则问题很可能是因为您忘记输入.py扩展名。 Just type in python ex14.py username and make sure you are in the directory your python scripts are stored in, then you should be all good. 只需输入python ex14.py username ,并确保您位于python脚本存储的目录中,那么一切都会很好。

If that isn't the problem then you should type out the exact error message you're receiving so people can be of more help. 如果这不是问题,那么您应该输入所收到的确切错误消息,以便人们提供更多帮助。 Happy coding! 编码愉快!

its working fine, you just mistyped in line #9, it should be 'likes' not 'lives' 它的工作正常,您只是在第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

the final script is to print all variable, and its expecting "likes" var. 最终的脚本是打印所有变量及其预期的“喜欢”变量。

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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM