我的while循环没有重复,即使没有满足哨兵值来打破循环。请帮助。在我看来,while循环看起来不应该破坏,直到变量computernumber等于数字
import random
print("Hello and welcome to the AI guessing Game pick a number from 1 to 100 then press H or L to give the clue")
number=input("pick a number from 1 to 100")
number=int(number)
tries = 0
computernumber=1
while computernumber !=(number):
computernumber=random.randint(1,100)
print (computernumber)
higherorlower=input("is your number higher or lower use H or L please!")
if higherorlower == "H":
computernumber +=1
tries = int(tries)
tries +=1
computernumber=random.randint (computernumber,number)
else:
computernumber -=1
computernumber=random.randint (1,computernumber)
tries = 0
tries = int(tries)
tries +=1
print (computernumber)
break
if computernumber == number:
if tries == 1 or 0:
print("Your number was", number,"and it took me",tries,"Try")
else:
print("Your number was", number,"and it took me",tries,"Tries")
答案 0 :(得分:0)
computernumber=random.randint(1,100)
这将在每次迭代中取范围(1,100)中的随机数。这就是为什么你不应该把它放在你的循环中
break
中的 else
将打破循环。这就是为什么你的循环不重复。
修改后的代码:
import random
print("Hello and welcome to the AI guessing Game pick a number from 1 to 100 then press H or L to give the clue")
number=input("pick a number from 1 to 100 :")
number=int(number)
tries = 0
computernumber=1
# Bring it outside of your loop.
computernumber=random.randint(1,100)
while computernumber !=(number):
print (computernumber)
higherorlower=input("is your number higher or lower use H or L please!")
if higherorlower == "H":
computernumber +=1
tries = int(tries)
tries +=1
computernumber=random.randint (computernumber,number)
else:
computernumber -=1
computernumber=random.randint (1,computernumber)
tries = 0
tries = int(tries)
tries +=1
# Do not use break here
#break
if computernumber == number:
if tries == 1 or 0:
print("Your number was", number,"and it took me",tries,"Try")
else:
print("Your number was", number,"and it took me",tries,"Tries")
答案 1 :(得分:0)
我必须修改程序中的一些内容才能使其正常工作。我为随机数添加了一个函数,并为匹配添加了M选项,因此循环结束。我还需要将输入函数更改为raw_input。这是因为输入修改了内容,而raw_input完全按照类型输入输入。
import random
def getRandomNumber( minimum, maximum ):
return random.randint( minimum, maximum )
print( "Hello and welcome to the AI guessing game. Pick a number from 1 to 100 then press H or L to give the clue.\n" )
number = int( raw_input("Pick a number from 1 to 100: ") )
tries = 0
computernumber = 0
# min and max random numbers initially
minimum = 1
maximum = 100
while computernumber != number:
tries = tries + 1
computernumber = getRandomNumber( minimum, maximum )
print (computernumber)
higherorlower = str( raw_input("Is your number higher, lower, or a match? Use H, L, or M please! ") )
if higherorlower == "H":
computernumber +=1
minimum = computernumber
maximum = number
elif higherorlower == "L":
computernumber -=1
minimum = 1
maximum = computernumber
elif higherorlower == "M":
computernumber = number # this ends the loop on a match
if computernumber == number:
if tries == 1 or 0:
print("Your number was", number,"and it took me",tries,"Try")
else:
print("Your number was", number,"and it took me",tries,"Tries")