gpt4 book ai didi

Python - 在使用 raw_input() 时遇到一些问题

转载 作者:行者123 更新时间:2023-11-28 21:26:19 25 4
gpt4 key购买 nike

所以我一直在研究一个两人“猜数字”程序。但我只是遇到了一件事。

代码如下:

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 周围加引号:choice =='yes' ,否则 python 会认为 yes 是一个变量。

关于Python - 在使用 raw_input() 时遇到一些问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13103806/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com