我想用Python编写一个简单的猜谜游戏。但是我想拥有一个附加功能,即范围(其中有正确的数字)在用户每次猜测后都会减小。我的代码如下:
com_number = 49
user_number = input('Enter the number from 0 to 100: ')
user_number = int(user_number)
check = 0
number_min = 0
number_max = user_number
while check == 0:
if user_number == com_number:
print(f'Congratulations!')
check = 1
elif user_number > com_number:
number_max = user_number
user_number = int(input(f'Too high... {number_min} < ? < {number_max}\n'))
else:
number_min = user_number
user_number = int(input(f'Too low... {number_min} < ? < {number_max}\n'))
脚本的示例输出:
Enter the number from 0 to 100: 89
Too high... 0 < ? < 89
75
Too high... 0 < ? < 75
12
Too low... 12 < ? < 75
46
Too low... 46 < ? < 75
66
Too high... 46 < ? < 66
58
Too high... 46 < ? < 58
52
Too high... 46 < ? < 52
47
Too low... 47 < ? < 52
50
Too high... 47 < ? < 50
48
Too low... 48 < ? < 50
49
Congratulations!
因此您可以看到当第一个猜测高于正确的数字(在这种情况下为89> 49)下,脚本可以按我的预期运行。
脚本的另一个示例输出:
Enter the number from 0 to 100: 68
Too high... 0 < ? < 68
30
Too low... 30 < ? < 68
45
Too low... 45 < ? < 68
50
Too high... 45 < ? < 50
49
Congratulations!
因此,当第二个猜测值低于正确数字时(在本例中为30 <49),脚本也可以正常工作。
但是问题是当第一个猜测低于正确数字时。在这种情况下,脚本还会更改print()语句中'number_max'变量的值,这是完全不正确的。请检查以下内容:
Enter the number from 0 to 100: 45
Too low... 45 < ? < 45
或
Enter the number from 0 to 100: 21
Too low... 21 < ? < 21
我不知道为什么会这样...
答案 0 :(得分:1)
尝试使用此代码代替
:com_number = 49
number_min, number_max = 0, 100
print(f'Enter number in between {number_min} < ? < {number_max}')
while True:
user_number = int(input())
if user_number < number_min or user_number > number_max:
print(f'Number not in range... {number_min} < ? < {number_max}')
elif user_number == com_number:
print('Congratulations!')
break
elif user_number > com_number:
number_max = user_number
print(f'To high... {number_min} < ? < {number_max}')
else:
number_min = user_number
print(f'To low... {number_min} < ? < {number_max}')
我已经简化了一些代码,因为这是不必要的,并修复了一个错误。我将解释我的更改:
# previously
check = 0
while check == 0:
# ...
check = 1
# now
while True:
# ...
break
while True
生成一个无限循环,只需在满足某些条件且无需再运行(当用户正确猜测时)的任何地方添加break
就可以中断无限循环。
# previously
number_min = 0
number_max = user_number
# now
number_min, number_max = 0, 100
a, b = 1, 2
一次分配两个变量(a = 1
和b = 2
)。 number_max = user_number
没有任何意义,是您所经历的怪异行为的原因。
if user_number < number_min or user_number > number_max:
是另一个边缘案例,它拒绝无效的用户输入。
答案 1 :(得分:0)
这是因为您在循环之前设置了number_max = user_number
。
解决该问题的一种方法是编写一条if
语句,或者只是指定number_max = 100
。
com_number = 49
user_number = input('Enter the number from 0 to 100: ')
user_number = int(user_number)
check = 0
number_min, number_max = 0, 100
while check == 0:
if user_number == com_number:
print(f'Congratulations!')
check = 1
elif user_number > com_number:
number_max = user_number
user_number = int(input(f'Too high... {number_min} < ? < {number_max}\n'))
else:
number_min = user_number
user_number = int(input(f'Too low... {number_min} < ? < {number_max}\n'))
答案 2 :(得分:0)
# lets have a random number generated instead of pre defining it.
import random
com_number = random.randint(1, 101)
user_number = int(input("Guess the number from 1 - 100 : "))
max_number = 100 # define max_number = 100 instead of o
min_number = 0
while user_number != com_number:
if user_number == com_number:
user_number = com_number
elif user_number > com_number:
max_number = user_number
user_number = int(input(f"Too high... {0} < ? < {max_number}\n"))
elif user_number < com_number:
min_number = user_number
highest = 100
user_number = int(input(f"Too low...{min_number} < ? < {max_number}\n"))
if user_number == com_number:
print("Congratulations.")
# I printed congratulations outside the loop because the loop will terminate after u
# guessed the number.
答案 3 :(得分:0)
我的问题的解决方案是如此明显……我不敢相信我在循环之前错过了这个声明:)。但是我在这段代码中发现了另一个问题。请检查输出:
Enter the number from 0 to 100: 51
Too high... 0 < ? < 51
99
Too high... 0 < ? < 99
这是错误的,很明显,在这种情况下,范围应该保持在0到51之间(即使用户输入了99号)
所以我不得不添加另一个条件。我注意到了您的所有建议,我的最终脚本如下所示。
import random
def guess():
max_range = int(input('Set the maximum range you want to guess the number:'))
com_number = random.randrange(0,max_range)
user_number = int(input(f'Enter the number from 0 to {max_range}:'))
i = 1
number_min = 0
number_max = max_range
while True:
if user_number == com_number:
break
elif user_number > com_number:
if user_number >= number_max:
number_max = number_max
else:
number_max = user_number
user_number = int(input(f'Too high... {number_min} < ? < {number_max}\n'))
i = i + 1
else:
if user_number < number_min:
number_min = number_min
else:
number_min = user_number
user_number = int(input(f'Too low... {number_min} < ? < {number_max}\n' ))
i = i + 1
print(f'Congratulations! You guessed the number at the {i} time.')
guess()
现在脚本从一开始就按我的意愿工作。谢谢您的帮助。
但是我想知道是否有一种方法可以使它更简单并实现相同的目标?
答案 4 :(得分:0)
这是一个了不起的问题。这就是我解决的方法:我只是简单地再次调用相同的函数,直到获得所需的结果并更新范围:
btw:如果您使用的是Python <3.6,请将f格式从f'{min}和{max}'更改为'{min}和{max}'。format(min = min,max = max)
import random
def guess(min,max):
n = int(input('Guess number: '))
if choice > n >= min:
min = n
print(f'Low: {min} > ? > {max}')
guess(min,max)
elif choice < n <= max:
max = n
print(f'High: {min} > ? > {max}')
guess(min,max)
else:
if choice == n:
print(f'Bravo! You got it {choice}')
else:
if choice > n:
print(f'Still Low: {min} > ? > {max}: check the scale!')
guess(min,max)
elif choice < n:
print(f'Still High: {min} > ? > {max}: check the scale!')
guess(min,max)
if __name__ == '__main__':
choice = random.randint(1,101)
min,max = 0, 100
guess(min,max)