Python - 在raw_input()上遇到一些麻烦

时间:2012-10-27 20:15:13

标签: python 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)

2 个答案:

答案 0 :(得分:4)

我猜您需要yeschoice =='yes'周围的引号,否则python会认为yes是变量。

答案 1 :(得分:2)

尝试if choice == 'yes':。您将其与未定义的符号进行比较。

请注意,Python中没有“变量”,只有符号及其值(语言解释器在内部可以理解它们的类型)。将它们视为您为各种对象提供的名称。它们都是符号。