while循环没有被执行
while loop is not being executed
在此代码中,用户要猜测计算机在 0 到 100 之间随机选择的一个数字。
问题是 while 循环根本没有执行。一切正常,直到我将该代码块放入 while 循环以使其重复,直到用户猜测数字或用完尝试为止。如何让 while 循环工作?我是 python 的初学者。
import random
def guessing_game():
print('''Welcome to the Number Guessing Game!
I'm thinking of a number between 1 and 100.''')
select_level = input("Choose a difficulty. Type 'easy' or 'hard': easy: ")
if select_level == "easy":
attempt_left = 10
print("You have 10 attempts remaining to guess the number.")
elif select_level == "hard":
attempt_left = 5
print("You have 5 attempts remaining to guess the number.")
computer_choice = random.randint(0,100)
#print(f"Pssst, the correct answer is {computer_choice}")
number_guessed = False
while number_guessed:
user_choice = int(input("Please enter a number between 0 and 100: "))
if computer_choice == user_choice:
number_guessed = True
print(f"You got it! The answer was {computer_choice}")
else:
attempt_left -= 1
if user_choice > computer_choice:
print(f"That is too high!\nGuess again.\nYou have {attempt_left} attempts remaining to guess the number.")
else:
print(f"That is too low!\nGuess again.\nYou have {attempt_left} attempts remaining to guess the number.")
if attempt_left == 0:
number_guessed = True
print("You've run out of guesses, you lose.")
guessing_game()
这应该有效:
import random
def guessing_game():
print('''Welcome to the Number Guessing Game!
I'm thinking of a number between 1 and 100.''')
select_level = input("Choose a difficulty. Type 'easy' or 'hard': easy: ")
if select_level == "easy":
attempt_left = 10
print("You have 10 attempts remaining to guess the number.")
elif select_level == "hard":
attempt_left = 5
print("You have 5 attempts remaining to guess the number.")
computer_choice = random.randint(0,100)
#print(f"Pssst, the correct answer is {computer_choice}")
number_guessed = False
while number_guessed == False:
user_choice = int(input("Please enter a number between 0 and 100: "))
if computer_choice == user_choice:
number_guessed = True
print(f"You got it! The answer was {computer_choice}")
else:
attempt_left -= 1
if user_choice > computer_choice:
print(f"That is too high!\nGuess again.\nYou have {attempt_left} attempts remaining to guess the number.")
else:
print(f"That is too low!\nGuess again.\nYou have {attempt_left} attempts remaining to guess the number.")
if attempt_left == 0:
number_guessed = True
print("You've run out of guesses, you lose.")
guessing_game()
你的代码的错误是,当你使用像 while somevariable
和 somevariable
等于 False
的 while
循环时,while
循环不会运行。你也可以试试 while not number_guessed
numbers_guessed
为假,while循环必须为运行为真。所以改变while循环或变量。
代码:
import random
def guessing_game():
print('''Welcome to the Number Guessing Game!
I'm thinking of a number between 1 and 100.''')
select_level = input("Choose a difficulty. Type 'easy' or 'hard': easy: ")
if select_level == "easy":
attempt_left = 10
print("You have 10 attempts remaining to guess the number.")
elif select_level == "hard":
attempt_left = 5
print("You have 5 attempts remaining to guess the number.")
computer_choice = random.randint(0,100)
#print(f"Pssst, the correct answer is {computer_choice}")
number_guessed = False
while not number_guessed:
user_choice = int(input("Please enter a number between 0 and 100: "))
if computer_choice == user_choice:
number_guessed = True
print(f"You got it! The answer was {computer_choice}")
else:
attempt_left -= 1
if user_choice > computer_choice:
print(f"That is too high!\nGuess again.\nYou have {attempt_left} attempts remaining to guess the number.")
else:
print(f"That is too low!\nGuess again.\nYou have {attempt_left} attempts remaining to guess the number.")
if attempt_left == 0:
number_guessed = True
print("You've run out of guesses, you lose.")
guessing_game()
你定义number_guessed
为False
,所以循环根本不执行。试试 while not number_guessed
.
在此代码中,用户要猜测计算机在 0 到 100 之间随机选择的一个数字。 问题是 while 循环根本没有执行。一切正常,直到我将该代码块放入 while 循环以使其重复,直到用户猜测数字或用完尝试为止。如何让 while 循环工作?我是 python 的初学者。
import random
def guessing_game():
print('''Welcome to the Number Guessing Game!
I'm thinking of a number between 1 and 100.''')
select_level = input("Choose a difficulty. Type 'easy' or 'hard': easy: ")
if select_level == "easy":
attempt_left = 10
print("You have 10 attempts remaining to guess the number.")
elif select_level == "hard":
attempt_left = 5
print("You have 5 attempts remaining to guess the number.")
computer_choice = random.randint(0,100)
#print(f"Pssst, the correct answer is {computer_choice}")
number_guessed = False
while number_guessed:
user_choice = int(input("Please enter a number between 0 and 100: "))
if computer_choice == user_choice:
number_guessed = True
print(f"You got it! The answer was {computer_choice}")
else:
attempt_left -= 1
if user_choice > computer_choice:
print(f"That is too high!\nGuess again.\nYou have {attempt_left} attempts remaining to guess the number.")
else:
print(f"That is too low!\nGuess again.\nYou have {attempt_left} attempts remaining to guess the number.")
if attempt_left == 0:
number_guessed = True
print("You've run out of guesses, you lose.")
guessing_game()
这应该有效:
import random
def guessing_game():
print('''Welcome to the Number Guessing Game!
I'm thinking of a number between 1 and 100.''')
select_level = input("Choose a difficulty. Type 'easy' or 'hard': easy: ")
if select_level == "easy":
attempt_left = 10
print("You have 10 attempts remaining to guess the number.")
elif select_level == "hard":
attempt_left = 5
print("You have 5 attempts remaining to guess the number.")
computer_choice = random.randint(0,100)
#print(f"Pssst, the correct answer is {computer_choice}")
number_guessed = False
while number_guessed == False:
user_choice = int(input("Please enter a number between 0 and 100: "))
if computer_choice == user_choice:
number_guessed = True
print(f"You got it! The answer was {computer_choice}")
else:
attempt_left -= 1
if user_choice > computer_choice:
print(f"That is too high!\nGuess again.\nYou have {attempt_left} attempts remaining to guess the number.")
else:
print(f"That is too low!\nGuess again.\nYou have {attempt_left} attempts remaining to guess the number.")
if attempt_left == 0:
number_guessed = True
print("You've run out of guesses, you lose.")
guessing_game()
你的代码的错误是,当你使用像 while somevariable
和 somevariable
等于 False
的 while
循环时,while
循环不会运行。你也可以试试 while not number_guessed
numbers_guessed
为假,while循环必须为运行为真。所以改变while循环或变量。
代码:
import random
def guessing_game():
print('''Welcome to the Number Guessing Game!
I'm thinking of a number between 1 and 100.''')
select_level = input("Choose a difficulty. Type 'easy' or 'hard': easy: ")
if select_level == "easy":
attempt_left = 10
print("You have 10 attempts remaining to guess the number.")
elif select_level == "hard":
attempt_left = 5
print("You have 5 attempts remaining to guess the number.")
computer_choice = random.randint(0,100)
#print(f"Pssst, the correct answer is {computer_choice}")
number_guessed = False
while not number_guessed:
user_choice = int(input("Please enter a number between 0 and 100: "))
if computer_choice == user_choice:
number_guessed = True
print(f"You got it! The answer was {computer_choice}")
else:
attempt_left -= 1
if user_choice > computer_choice:
print(f"That is too high!\nGuess again.\nYou have {attempt_left} attempts remaining to guess the number.")
else:
print(f"That is too low!\nGuess again.\nYou have {attempt_left} attempts remaining to guess the number.")
if attempt_left == 0:
number_guessed = True
print("You've run out of guesses, you lose.")
guessing_game()
你定义number_guessed
为False
,所以循环根本不执行。试试 while not number_guessed
.