简体   繁体   中英

Python - Having some trouble with raw_input()

So I have been working on a two player "guess the number" program. But im just having trouble with one thing.

So here's code:

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!"

Everything is running smoothly until I get this error:

Traceback (most recent call last):
  File "C:\Python27\Script 1", line 11, in <module>
    if choice == yes:
NameError: name 'yes' is not defined

To my knowledge, I don't think I have done anything wrong, but then again I am a beginner to python.

Someone please help me with this.

EDIT: (This is python 2.7 if it makes a difference)

我猜你需要在yes周围yes引号: choice =='yes' ,否则python会认为yes是一个变量。

try if choice == 'yes': . You're comparing it with a symbol that isn't defined.

Note there are no 'variables' in Python, only symbols and their values(with their types internally understood by the language interpreter). Think of them as names you give to various objects. They're all symbols.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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