[英]How do I break out of my while loop without using the most popular functions?(sys.exit,break,etc.)
我正在制作一個生成隨機數的程序,並要求您猜測 1-100 范圍之外的數字。 輸入數字后,它會根據該數字生成響應。 在這種情況下,如果輸入為 0,則Too high
、 Too low
、 Correct
或Quit too soon
結束程序(簡化,但基本相同)。
它根據您必須輸入 function 的次數來計算嘗試次數,並使用 while 循環不斷詢問數字,直到您輸入正確為止。 我面臨的問題是,一旦猜測等於隨機數或0
,我就必須讓它跳出 while 循環。 這通常不是問題,因為您可以使用sys.exit()
或其他一些 function,但根據說明我不能使用break
、 quit
、 exit
、 sys.exit
或continue
。 問題是我找到的大多數解決方案都是為了打破 while 循環實現break
、 sys.exit
或類似的東西,但我不能使用它們。 不過,我使用sys.exit()
作為占位符,以便它運行代碼的 rest,但現在我需要找出一種不使用它來中斷循環的方法。 這是我的代碼:
import random
import sys
def main():
global attempts
attempts = 0
guess(attempts)
keep_playing(attempts)
def guess(attempts):
number = random.randint(1,100)
guess = int(input("Enter a number between 1 and 100, or 0 to quit: "))
while guess != 0:
if guess != number:
if guess < number:
print("Too low, try again")
attempts += 1
guess = int(input("Enter a number between 1 and 100, or 0 to quit: "))
elif guess > number:
print("Too high, try again")
attempts += 1
guess = int(input("Enter a number between 1 and 100, or 0 to quit: "))
else:
print()
print("Congratulations! You guessed the right number!")
print("There were", attempts,"attempts")
print()
#Ask if they want to play again
sys.exit()#<---- using sys.exit as a placeholder currently
else:
print()
print("You quit too early")
print("The number was ",number,sep='')
#Ask if they want to play again
sys.exit()#<----- using sys.exit as a placeholder currently
def keep_playing(attempts):
keep_playing = 'y'
if keep_playing == 'y' or keep_playing == 'n':
if keep_playing == 'y':
guess(attempts)
keep_playing = input("Another game (y to continue)? ")
elif keep_playing == 'n':
print()
print("You quit too early")
print("Number of attempts", attempts)
main()
如果有人對如何解決此問題有任何建議或解決方案,請告訴我。
嘗試對您的代碼實施此解決方案:
is_playing = True
while is_playing:
if guess == 0:
is_playing = False
your code...
else:
if guess == number:
is_playing = False
your code...
else:
your code...
不使用任何 break 等,它確實會跳出循環,因為只有在 is_playing 為 True 時循環才會繼續。 這樣,當猜測為 0(您的簡單退出方式)或猜對數字時,您將跳出循環。 希望有所幫助。
我不是全局變量的粉絲,但這是您實現我的解決方案的代碼:
import random
def main() -> None:
attempts = 0
global is_playing
is_playing = True
while is_playing:
guess(attempts)
keep_playing()
def guess(attempts: int) -> None:
number = random.randint(1,100)
print(number)
is_guessing = True
while is_guessing:
attempts += 1
guess = int(input("Enter a number between 1 and 100, or 0 to quit: "))
if guess == 0:
is_guessing = False
print("\nYou quit too early.")
print("The number was ", number,sep='')
else:
if guess == number:
is_guessing = False
print("\nCongratulations! You guessed the right number!")
print("There were", attempts, "attempts")
else:
if guess < number:
print("Too low, try again.")
elif guess > number:
print("Too high, try again.")
def keep_playing() -> None:
keep_playing = input('Do you want to play again? Y/N ')
if keep_playing.lower() == 'n':
global is_playing
is_playing = False
main()
提示:而不是"There were", attempts, "attempts"
做: f'There were {attempts} attempts.'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.