Python不接受我的输入,我做错了什么?

问题描述 投票:0回答:1

我正在为主脑游戏创建代码,我认为一切都应该是完美的,但是当我尝试编写第一个输入所需的信息时(在这种情况下,像“R”这样的颜色,这对我来说无关紧要这会直接跳到打印(“你必须引入四种颜色,不多也不少!”)

有人可以帮我吗???

这是代码:

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
python input
1个回答
1
投票

用打印进行一些轻量级的调试,你会发现代码完全符合你的要求:它不打印(“你必须引入四种颜色,不多也不少!”)当长度是同样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! 
© www.soinside.com 2019 - 2024. All rights reserved.