我正在嘗試使用 python 3.9 進行動物測驗。在其中一個問題上錯了 3 次并不會像應該那樣開始一個新問題。相反,它只是空白。在此人的第一次和第二次嘗試中,一切都很順利。任何和所有的幫助表示贊賞。這是我的代碼:
def check_guess(guess, answer):
global score
global name
still_guessing = True
attempt = 0
while still_guessing and attempt < 3:
if guess == answer:
print('Correct wow, i expected worse from someone named %s' % name)
if attempt == 0:
score = score 3
elif attempt == 1:
score = score 2
elif attempt == 2:
score = score 1
still_guessing = False
elif attempt <= 1:
print('HAAAAAAAAAAAAAAAAAAAAAAAAAAAA IMAGINE NOT GETTING THAT RIGHT!!!')
print('L bozo ratio')
guess = input('Try again ')
attempt = attempt 1
if attempt == 3:
print('the correct answer was %s' % answer)
print('There is always next time!!')
still_guessing = False
score = 0
print('Welcome to the animal quiz')
print()
print('In this game you will have to guess the animal')
name = input('What is your name? ')
print('Cool name, I feel like i heard of it before hmmm %s..' % name)
print()
print('Enough stalling onto the game!!!')
guess1 = input('Which bear lives in the north pole ')
check_guess(guess1.strip().lower(), 'polar bear')
uj5u.com熱心網友回復:
你必須把它if attempt == 3放在你的while回圈中,因為回圈while無限運行直到猜測是正確的,所以當attempt'的值為3時,它什么都不做,因為它仍然在一個回圈中并且沒有if陳述句告訴它什么一旦值為 3 就做。
編輯:還將回圈條件更改為still guessing and attempt <= 3
attempt = 0
def check_guess(guess, answer):
global score
global name
global attempt
still_guessing = True
while still_guessing and attempt <= 3:
print(f"attempt {attempt}")
if attempt == 2:
print('the correct answer was %s' % answer)
print('There is always next time!!')
still_guessing = False
else:
print('HAAAAAAAAAAAAAAAAAAAAAAAAAAAA IMAGINE NOT GETTING THAT RIGHT!!!')
print('L bozo ratio')
guess = input('Try again ')
attempt = attempt 1
if guess == answer:
print('Correct wow, i expected worse from someone named %s' % name)
if attempt == 0:
score = score 3
elif attempt == 1:
score = score 2
elif attempt == 2:
score = score 1
still_guessing = False
uj5u.com熱心網友回復:
我認為elif波紋管應該評估 <= 2,而不是 1:
elif attempt <= 2:
但是仍然列印最后一個“再試一次”訊息。您可以在“再試一次”訊息之前放置一個嘗試檢查條件來解決這個問題。如果條件評估為 True,則中斷回圈:
elif attempt <= 2:
print('HAAAAAAAAAAAAAAAAAAAAAAAAAAAA IMAGINE NOT GETTING THAT RIGHT!!!')
print('L bozo ratio')
attempt = attempt 1
if attempt > 2:
break
guess = input('Try again ')
在這種情況下,請記住調整您的 While 條件,因為不再需要嘗試檢查。
如果可以的話,我對代碼進行了一些重構,因此您可以查看其他方法來實作相同的目標。
def check_guess(guess, answer, attempts):
global score
global name
while True:
if guess == answer:
print('Correct wow, i expected worse from someone named %s' % name)
score = [0, 1, 2, 3]
final_score = score[attempts]
print(f'Score: {final_score}')
break
attempts -= 1
if attempts == 0:
print('the correct answer was %s' % answer)
print('There is always next time!!')
break
print('HAAAAAAAAAAAAAAAAAAAAAAAAAAAA IMAGINE NOT GETTING THAT RIGHT!!!')
print('L bozo ratio')
guess = input('Try again ')
score = 0
print('Welcome to the animal quiz')
print()
print('In this game you will have to guess the animal')
name = input('What is your name? ')
print('Cool name, I feel like i heard of it before hmmm %s..' % name)
print()
print('Enough stalling onto the game!!!')
guess1 = input('Which bear lives in the north pole ')
check_guess(guess1.strip().lower(), 'polar bear', attempts=3)
uj5u.com熱心網友回復:
使用下面的代碼對我有用。
def check_guess(guess, answer):
global score
global name
still_guessing = True
attempt = 0
while still_guessing and attempt < 3:
if guess == answer:
print('Correct wow, i expected worse from someone named %s' % name)
if attempt == 0:
score = score 3
elif attempt == 1:
score = score 2
elif attempt == 2:
score = score 1
still_guessing = False
elif attempt <= 2:
print('HAAAAAAAAAAAAAAAAAAAAAAAAAAAA IMAGINE NOT GETTING THAT RIGHT!!!')
print('L bozo ratio')
guess = input('Try again ')
attempt = attempt 1
if attempt == 3:
print('the correct answer was %s' % answer)
print('There is always next time!!')
still_guessing = False
score = 0
print('Welcome to the animal quiz')
print()
print('In this game you will have to guess the animal')
name = input('What is your name? ')
print('Cool name, I feel like i heard of it before hmmm %s..' % name)
print()
print('Enough stalling onto the game!!!')
guess1 = input('Which bear lives in the north pole ')
check_guess(guess1.strip().lower(), 'polar bear')
uj5u.com熱心網友回復:
在你的while回圈中'嘗試'永遠不會變成3,所以代碼不能跳轉到下一部分if attmpt == 3
所以在while回圈中,elif條件應該是elif attempt <= 2:可以 attempt = attempt 1達到3
uj5u.com熱心網友回復:
我注意到一些錯誤。首先,您不需要 if attempt == 3 或 2 因為您使用的是 while 回圈(while 回圈首先基于條件)。其次,最好將“break”整合為沒有無限回圈。While 回圈從 0 開始,到 2 結束(取 3 個值)。
我為你重寫了代碼。
def check_guess(guess, answer):
global score
global name
still_guessing = True
attempt = 0
while still_guessing and attempt < 2:
if guess == answer:
print('Correct wow, i expected worse from someone named %s' % name)
if attempt == 0:
score = score 3
elif attempt == 1:
score = score 2
elif attempt == 2:
score = score 1
still_guessing = False
elif attempt <= 1:
print('HAAAAAAAAAAAAAAAAAAAAAAAAAAAA IMAGINE NOT GETTING THAT RIGHT!!!')
print('L bozo ratio')
guess = input('Try again ')
attempt = attempt 1
break
print('the correct answer was %s' % answer)
print('There is always next time!!')
still_guessing = False
要測驗代碼添加print("pass OK")
score = 0
print('Welcome to the animal quiz')
print()
print('In this game you will have to guess the animal')
name = input('What is your name? ')
print('Cool name, I feel like i heard of it before hmmm %s..' % name)
print()
print('Enough stalling onto the game!!!')
guess1 = input('Which bear lives in the north pole ')
check_guess(guess1.strip().lower(), 'polar bear')
print("pass OK")
不要忘記投票:)(齋月卡里姆!!!)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/454868.html
