每当我运行这个时,我都会得到一个无限循环,打印出“更高”,我尝试移动东西并使用缩进,但无济于事
# Guess My Number
#
# The computer picks a random number between 1 and 100
# The player tries to guess it and the computer lets
# the player know if the guess is too high, too low # or right on the money
import random
#set initial values
print("This is guess my number\nI'm thinking of a number between 0-100")
#generate number
def ask_number():
"""Ask for a number within a range."""
the_number = random.randint(1,100)
guess = int(float())
tries = 1
question = (input("Take a guess: "))
print(question)
while guess != the_number:
if guess < the_number:
print("Higher...")
else:
print("Lower...")
guess = int(input("try again: "))
tries += 1
if guess == the_number:
print("Well done, the number was", the_number)
print("It took you", tries, "attempt/s")
ask_number()
input("\n\nPress enter key to exit")
Run Code Online (Sandbox Code Playgroud)
while guess != the_number:
if guess < the_number:
print("Higher...")
else:
print("Lower...")
Run Code Online (Sandbox Code Playgroud)
该循环中没有任何input()语句,因此guess保持不变,因此循环将永远运行。