【发布时间】:2020-12-29 15:31:20
【问题描述】:
我的程序有问题。我正在开发一个程序,可以让你玩猜正确数字的小游戏。问题是如果你猜对了,它不会打印出来:“你猜对了”。该程序将不会继续,并将停留在正确的号码上。只有当您必须多次猜测时才会发生这种情况。我尝试将 else 更改为 break 命令,但没有成功。 有人给点建议吗?
这是我用来测试它的:
最小的数:1
最大数:10
你能猜到多少次:10
如果您尝试猜测正确的数字 2 或 3 次(如果您需要更多猜测,可能更多)它不会打印出您赢了。
import random
#counts the mistakes
count = 1
#askes to give up a minimum and maximum to guess between
minimum = int(input("what is the smallest number? "))
maximum = int(input("what is the biggest number? "))
#askes how many times u can guess in total
amount = int(input("How many times can you guess? "))
#random number between the 2 variables minimum and maximum
x = random.randrange(minimum, maximum)
guess = int(input("guess the number: "))
#while loop until the guess is the same as the random number
while guess != x:
#this is if u guessed to much u get the error that you've guessed to much
while count < amount:
if guess > x:
print("this is not the correct number, the correct number is lower \n")
guess = int(input("guess the number: "))
count += 1
elif guess < x:
print("this is not the correct number, the correct number is higher \n")
guess = int(input("guess the number: "))
count += 1
else: print("\n \nYou Lost, You've guessed", x, "times\n")
break
#this part is not working, only if you guess it at the first time. it should also print this if you guessed it in 3 times
else: print("You guessed it correctly", x)
test = (input("this is just a test if it continues out of the loop "))
print(test)
【问题讨论】:
标签: python