调试Mastermind代码

时间:2017-01-17 11:56:34

标签: python python-3.x debugging

基本上发生的事情是当我运行此代码并输入数字来猜测数字是什么时

e.g。 1000它会说

  

"你有0个数字!"

表示第二个数字为1.

  

我该如何解决这个问题?

print("You have selected Hrd mode.")
randomNumber = GetNumber()
userChoice = [int(i) for i in list(input("Enter 4 numbers.\n"))]
while len(userChoice) != 4:
    userChoice = [int(i) for i in list(input("Enter 4 numbers.\n"))]
while userChoice != randomNumber:
    hits = [str(i+1) for i in range(4) if userChoice[i] == randomNumber[1]]
    many = (sum(n == c for n, c in zip(randomNumber, userChoice)))
    if hits:
        print("You got "+str(many)+" numbers right!")
    else:
        print("You got none right")
    userChoice = [int(i) for i in list(input("Enter 4 numbers.\n"))]
    while len(userChoice) != 4:
        userChoice = [int(i) for i in list(input("Enter 4 numbers.\n"))]
print("Congratulations! You got it right.")

1 个答案:

答案 0 :(得分:0)

变量hits在这里似乎没用,删除它并使用变量many检查用户是否找到了什么。

print("You have selected Hrd mode.")
randomNumber = GetNumber()
userChoice = [int(i) for i in list(input("Enter 4 numbers.\n"))]
while len(userChoice) != 4:
    userChoice = [int(i) for i in list(input("Enter 4 numbers.\n"))]
while userChoice != randomNumber:
    many = (sum(n == c for n, c in zip(randomNumber, userChoice)))
    if many != 0:
        print("You got "+str(many)+" numbers right!")
    else:
        print("You got none right")
    userChoice = [int(i) for i in list(input("Enter 4 numbers.\n"))]
    while len(userChoice) != 4:
        userChoice = [int(i) for i in list(input("Enter 4 numbers.\n"))]
print("Congratulations! You got it right.")