我正在创建一个程序,它要求您从0 to 100
中想出一个数字。然后它将猜测它是否是50, lower than 50, or higher than 50
。程序将一直用不同的数字猜测,直到猜出正确的数字为止。在
如果用户没有输入yes
、lower
或{
以下是我目前所拥有的(我是一个新手程序员,如果这没有意义或者我的代码很糟糕,我很抱歉!)公司名称:
print('Hello.')
print('Pick a secret number between 0 and 100.')
print('Is your secret number 50')
low = 0
high = 101
guess = 50
a = input('Enter yes/higher/lower:\n')
while True:
if a == 'yes':
print('Great!')
break
elif a == 'lower':
high = guess
guess1 = (guess-low)//2+low
print('Next is', guess1)
print('Is your secret number', guess1)
elif a == 'higher':
low = guess
guess1 = (high-guess)//2+guess
print('Next is', guess1)
print('Is your secret number', guess1)
elif a != 'yes' or 'higher' or 'lower':
print('I did not understand')
试试这段代码,看看这是否是程序的预期行为。在
算法如下:
low
和high
值的middle
进行猜测。所以你用guess = int((high+low)/2)
来做这个guess
,则将low
值改为guess+1
。如果用户的数字小于您的guess
,那么您将high
值改为guess-1
。在相关问题 更多 >
编程相关推荐