Python-无法打印你赢了

时间:2014-10-26 18:08:27

标签: python

似乎无法打印出来"恭喜,你是对的"在猜测正确之后。

来自随机导入randint

guessesTaken = 0

randomNumber = [str(randint(1,9))for _ in range(4)] #create of random nums

而guessesTaken< 10:

guesses = list(input("Guess Number: ")) # create list of four digits
check = "".join([b if a==b else "H" if int(a)>int(b) else "L" for a, b in zip(guesses,randomNumber)])
if all(c == 'Y' for c in check):  # if check has four Y's we have a correct guess
    print("Congratulations, you are correct, it took you", guessesTaken, "guesses.")
    break
else:
    guessesTaken += 1 # else increment guess count and ask again
    print(check)
if guessesTaken == 10:
    print("You lose")

1 个答案:

答案 0 :(得分:0)

这里发生的事情是因为check是一个字符串而randomNumber是一个列表,比较它们将始终返回False。您需要更改其中一个的类型,以便按照您想要的方式进行比较。将if语句更改为:

if check == ''.join(randomNumber):