因此,作为Python的一部分,我目前正在进行的一项练习是创建一个在终端中运行的猜谜游戏。显然,有多种方法可以做到这一点,而不是简单地观看解决方案视频,我首先尝试自己做。我的代码运行没有错误,据我所知它应该完成这项工作,但事实并非如此。而不只是在这里查找解决方案并重写整个事情,只是复制别人所做的事情,我想知道是否有人可以帮助我理解为什么我的代码没有达到我的预期呢?
下面列出了,谢谢。
import random
digits = list(range(10))
random.shuffle(digits)
one = digits[0:1]
two = digits[1:2]
three = digits[2:3]
digits = one, two, three
guess = input("What is your guess? ")
g_one = guess[0:1]
g_two = guess[1:2]
g_three = guess[2:3]
while(guess != digits):
print(digits)
if(guess == digits):
print("Congrats, you guessed correctly")
break
elif(g_one == one or g_two == two or g_three == three):
print("One or more of the numbers is correct")
guess = input("Try again: ")
elif(g_one != one and g_two != two and g_three != three):
print("None of those numbers are correct.")
guess = input("Try again: ")
else:
break
答案 0 :(得分:0)
您似乎没有在每次迭代时更新g_one,g_two和g_three的值。
digits = ([2], [3], [4]) # Assume this is the shuffling result, You are getting a tuple of lists here
guess = input("What is your guess? ") # 123
g_one = guess[0:1] #1
g_two = guess[1:2] #2
g_three = guess[2:3] #3
while(guess != digits): #123 != ([2], [3], [4]) string vs a tuple comparison here
print(digits)
if(guess == digits):
print("Congrats, you guessed correctly")
break
elif(g_one == one or g_two == two or g_three == three):
print("One or more of the numbers is correct")
guess = input("Try again: ") #You are getting the input but the splits are not updated
elif(g_one != one and g_two != two and g_three != three):
print("None of those numbers are correct.")
guess = input("Try again: ") #Same here. Not updating the splits
else:
break
我认为应该解释一下。