Python没有';I';我做错了吗?
我正在为智囊团游戏编写代码,我认为一切都应该是完美的,但是当我尝试编写第一次输入所需的信息时(在这种情况下,像“R”这样的颜色),无论我做什么,都会直接跳转到 印刷(“你必须引入四种颜色,不多不少!”) 有人能帮我吗 代码如下:Python没有';I';我做错了吗?,python,input,Python,Input,我正在为智囊团游戏编写代码,我认为一切都应该是完美的,但是当我尝试编写第一次输入所需的信息时(在这种情况下,像“R”这样的颜色),无论我做什么,都会直接跳转到 印刷(“你必须引入四种颜色,不多不少!”) 有人能帮我吗 代码如下: colours=["R", "G", "B", "Y", "W", "R", "P"] attempts=0 game=True codemaker=random.sample(colours,4) print (codemaker) while game:
colours=["R", "G", "B", "Y", "W", "R", "P"]
attempts=0
game=True
codemaker=random.sample(colours,4)
print (codemaker)
while game:
white_points=0
black_points=0
guessed_colour=""
player_guess=input().upper()
attempts+=1
if len(player_guess) != len(codemaker):
print ("You have to introduce four colours, no more, no less! ")
continue
使用print进行一点轻量级调试,您将看到代码完全按照您要求的那样执行:当长度相同时,它不会打印(“您必须引入四种颜色,不能多,不能少!”)。4:
import random
colours=["R", "G", "B", "Y", "W", "R", "P"]
attempts=0
game=True
codemaker=random.sample(colours,4)
print(len(codemaker), codemaker)
while game:
white_points=0
black_points=0
guessed_colour=""
player_guess=input().upper()
attempts+=1
print(player_guess, len(player_guess))
if len(player_guess) != len(codemaker):
print("You have to introduce four colours, no more, no less! ")
continue
测试
4 ['W', 'B', 'R', 'G']
abcde
ABCDE 5
You have to introduce four colours, no more, no less!
WBRG
WBRG 4
@#$%^U
@#$%^U 6
You have to introduce four colours, no more, no less!
您在
input
中究竟输入了什么?您只输入了一个字母吗?我无法再现错误。我怀疑您的输入有错误。input
的结果是一个字符串。从您的代码中,您的输入必须是一个由4个字母组成的字符串,例如,rgby
,以便len
比较工作