[英]Python - Having some trouble with raw_input()
因此,我一直在研究兩個玩家“猜數字”程序。 但是我只是遇到一件事的麻煩。
所以這是代碼:
import time
import random
thenumber = random.randint(1, 10)
print "Welcome to Noah's Two Player guess a number game."
print "What is player one's name?"
player1 = raw_input()
print "What is player two's name?"
player2 = raw_input()
print "Well " + player1 + " and " + player2 + ", are you ready to play?"
choice = raw_input()
if choice == yes:
print player1 + ", pick a number from 1 to 10."
player1guess = raw_input()
print player2 + ", pick a number from 1 to 10."
player2guess = raw_input()
print "Calculating..."
time.sleep(3)
p1 = thenumber - player1guess
p2 = thenumber - player2guess
if p1 > p2:
print player1 + " won!"
elif p2 > p1:
print player2 + " won!"
一切正常,直到出現此錯誤:
Traceback (most recent call last):
File "C:\Python27\Script 1", line 11, in <module>
if choice == yes:
NameError: name 'yes' is not defined
就我所知,我認為我沒有做錯任何事情,但是我還是Python的初學者。
有人請幫我。
編輯:(這是python 2.7,如果有所作為)
我猜你需要在yes
周圍yes
引號: choice =='yes'
,否則python會認為yes
是一個變量。
嘗試if choice == 'yes':
。 您正在將其與未定義的符號進行比較。
請注意,Python中沒有“變量”,只有符號及其值(語言解釋器內部理解其類型)。 可以將它們視為您為各種對象賦予的名稱。 它們都是符號。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.