import random
# Define function - Number Generator
def num_gen():
num = random.randrange(1, 101)
return num
# Define function - Number Check
def num_check(x, y):
result = ''
if x > y:
result = 'high'
elif x < y:
result = 'low'
else:
result = 'correct'
return result
# Call - Number Generator
num = num_gen()
# Input - Guess
guess = int(input('Please guess a number between 1 and 100: '))
att = 1
# Process - Guess and Display Result
result = num_check(guess, num)
if result == 'high':
guess = int(input('Your guess was HIGH! Please guess another number between 1 and 100: '))
att = 1
result = num_check(guess, num)
elif result == 'low':
guess = int(input('Your guess was LOW! Please guess another number between 1 and 100: '))
att = 1
result = num_check(guess, num)
else:
print('Your guess was CORRECT! You got it in ' str(att) ' attempts!')
uj5u.com熱心網友回復:
從控制臺獲取輸入后,您應該創建一個回圈,當猜測的數字等于輸入時中斷。例如:
# Input - Guess
guess = int(input('Please guess a number between 1 and 100: '))
att = 1
# Process - Guess and Display Result
result = num_check(guess, num)
while result != guess:
if result == 'high':
guess = int(input('Your guess was HIGH! Please guess another number between 1 and 100: '))
att = 1
result = num_check(guess, num)
elif result == 'low':
guess = int(input('Your guess was LOW! Please guess another number between 1 and 100: '))
att = 1
result = num_check(guess, num)
else:
break
print('Your guess was CORRECT! You got it in ' str(att) ' attempts!')
修改后代碼如下:
import random
# Define function - Number Generator
def num_gen():
num = random.randrange(1, 101)
return num
# Define function - Number Check
def num_check(x, y):
result = ''
if x > y:
result = 'high'
elif x < y:
result = 'low'
else:
result = 'correct'
return result
# Call - Number Generator
num = num_gen()
# Input - Guess
guess = int(input('Please guess a number between 1 and 100: '))
att = 1
# Process - Guess and Display Result
result = num_check(guess, num)
while result != guess:
if result == 'high':
guess = int(input('Your guess was HIGH! Please guess another number between 1 and 100: '))
att = 1
result = num_check(guess, num)
elif result == 'low':
guess = int(input('Your guess was LOW! Please guess another number between 1 and 100: '))
att = 1
result = num_check(guess, num)
else:
break
print('Your guess was CORRECT! You got it in ' str(att) ' attempts!')
uj5u.com熱心網友回復:
這是因為python程式是從上到下運行的,到了底部就結束了,就停止運行了。要停止此訂單,我們有多種不同的方法:
我們可以呼叫函式,就像您在程式中所做的那樣。
選擇控制結構:Asif 陳述句
迭代控制結構:作為回圈
最后一個是您在此解決方案中需要的。用正確的條件將代碼包裝在一個while回圈中,在你的例子中,這將是
# Input - Guess
guess = int(input('Please guess a number between 1 and 100: '))
att = 1
# Process - Guess and Display Result
result = num_check(guess, num)
if result == 'high':
guess = int(input('Your guess was HIGH! Please guess another number between 1 and 100: '))
att = 1
result = num_check(guess, num)
elif result == 'low':
guess = int(input('Your guess was LOW! Please guess another number between 1 and 100: '))
att = 1
result = num_check(guess, num)
else:
print('Your guess was CORRECT! You got it in ' str(att) ' attempts!')
最好的方法是使用 will 回圈,如下所示:
while guess != num:
uj5u.com熱心網友回復:
你應該使用while True:, 因為你的猜測代碼在沒有它的情況下只運行一次
import random
# Define function - Number Generator
def num_gen():
num = random.randrange(1, 10)
return num
# Define function - Number Check
def num_check(x, y):
result = ''
if x > y:
result = 'high'
elif x < y:
result = 'low'
else:
result = 'correct'
return result
# Call - Number Generator
att = 1
num = num_gen()
while True:
# Input - Guess
guess = int(input('Please guess a number between 1 and 100: '))
# Process - Guess and Display Result
result = num_check(guess, num)
if result == 'high':
print('Your guess was HIGH! Please guess another number between 1 and 100: ')
att = 1
elif result == 'low':
print('Your guess was LOW! Please guess another number between 1 and 100: ')
att = 1
else:
print('Your guess was CORRECT! You got it in ' str(att) ' attempts!')
break
uj5u.com熱心網友回復:
如果您沒有在程式中使用任何回圈,您怎么能期望它運行超過 1 次。例如,在這種情況下,您應該使用while loop,然后只有它會一次又一次地提示用戶并檢查條件,將game變數設定為True。當數字匹配時,只需將游戲變數設定為False,它將結束回圈。
import random
# Set the game variable initially to be True
game = True
# Define function - Number Generator
def num_gen():
num = random.randrange(1, 101)
return num
# Define function - Number Check
def num_check(x, y):
result = ''
if x > y:
result = 'high'
elif x < y:
result = 'low'
else:
result = 'correct'
return result
# Call - Number Generator
num = num_gen()
# Input - Guess
guess = int(input('Please guess a number between 1 and 100: '))
att = 1
# Process - Guess and Display Result
result = num_check(guess, num)
while game:
if result == 'high':
guess = int(input('Your guess was HIGH! Please guess another number between 1 and 100: '))
att = 1
result = num_check(guess, num)
elif result == 'low':
guess = int(input('Your guess was LOW! Please guess another number between 1 and 100: '))
att = 1
result = num_check(guess, num)
else:
print('Your guess was CORRECT! You got it in ' str(att) ' attempts!')
game = False
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/409637.html
標籤:
