簡體   English   中英

學習python練習14不在命令提示符/ powershell中運行的困難方式

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

我正在努力學習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)  

我也在使用Windows 7和Notepad ++

腳本本身沒有什么問題,除了您的第一個raw_input變量應為“ raw_input ”時為“生命”。

在我看來,如果您嘗試通過鍵入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.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM