繁体   English   中英

试图了解我的Python猜测游戏代码

[英]Trying to understand my Python guessing game code

因此,作为此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

似乎您没有在每次迭代中更新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

我认为应该对此进行解释。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM