我正在練習 Python,但我的代碼遇到了一些問題。我正在嘗試定義主要功能并提示用戶輸入三個特定數字。該程式應該找到這三個數字的平均值并將平均值與輸入的數字進行比較,然后計算有多少數字等于平均值??。
這是我的代碼:
def main():
def introduction():
print("Welcome to My Program!")
print("My name is Alex.")
print("In this program, you will enter three numbers.")
print("The program will find the average of those numbers.")
print("The program will compare those three numbers to the average.")
print("Continue to the next section please....")
n1 = int(input("Please Enter Number 1: "))
n2 = int(input("Please Enter Number 2: "))
n3 = int(input("Please Enter Number 3: "))
print(f'The numbers you entered were: {n1} {n2} {n3}')
def findaverage(n1, n2, n3):
avg = (n1 n2 n3)/3
return avg
print(f'The average for those numbers are: {avg:.3f}')
def comparetoavg(a1, a2, a3, avg):
count = 0
number = [a1, a2, a3]
for x in number:
if number > avg:
print(f'This number: {number} is above than the average {avg:.3f}.')
elif number < avg:
print(f'This number: {number} is below than the average {avg:.3f}.')
else:
count = 1
print(f'This number: {number} is equal to the average {avg:.3f}.')
print(f'{count} values are equal to the average.')
main()
- 我的代碼不會通過。它只列印 3 次“請輸入號碼”并列印陳述句。
- 我希望我的代碼迭代 10 次。我知道必須使用回圈,但我不確定在哪里放置回圈。
- 我希望 2 次迭代使三個值等于平均值??,3 次沒有值等于平均值??,5 次使其中一個值等于平均值??。有人可以幫幫我嗎?
uj5u.com熱心網友回復:
您的代碼似乎有幾個問題。
您只呼叫 main 函式。主函式中只有函式定義,沒有函式呼叫。您必須將函式呼叫放在主函式中,然后呼叫主函式。充其量,您也可以將函式定義放在主函式定義之上。
您可以使用 input() 函式來請求用戶輸入。請參閱下面的示例。
如果要呼叫函式 10 次,則必須將它們的函式呼叫放在具有 10 次迭代的 for 回圈體中。請參見下面的示例。
試試這個:
def findaverage(n1, n2, n3):
...
def comparetoavg(a1, a2, a3, avg):
...
def main():
for i in range(0, 10):
n1 = input("Enter number:")
n2 = input("Enter number:")
n3 = input("Enter number:")
avg = findaverage(n1, n2, n3)
comparetoavg(n1, n2, n3, avg)
main()
這只是一個關于如何使用 for 回圈、輸入函式以及如何正確定義和呼叫方法的示例。您可能需要進一步調整它以滿足您的需求。
uj5u.com熱心網友回復:
您需要呼叫您的函式,以便它們實際運行并確保它們在同一范圍內,以便它們實際上是可呼叫的
def findaverage(n1, n2, n3):
avg = (n1 n2 n3)/3
print(f'The average for those numbers are: {avg:.3f}')
return avg
def comparetoavg(a1, a2, a3, avg):
count = 0
number = [a1, a2, a3]
for x in number:
if x > avg:
print(f'This number: {x} is above than the average {avg:.3f}.')
elif x < avg:
print(f'This number: {x} is below than the average {avg:.3f}.')
else:
count = 1
print(f'This number: {x} is equal to the average {avg:.3f}.')
print(f'{count} values are equal to the average.')
def introduction():
print("Welcome to My Program!")
print("My name is Alex.")
print("In this program, you will enter three numbers.")
print("The program will find the average of those numbers.")
print("The program will compare those three numbers to the average.")
print("Continue to the next section please....")
def main():
introduction()
n1 = int(input("Please Enter Number 1: "))
n2 = int(input("Please Enter Number 2: "))
n3 = int(input("Please Enter Number 3: "))
print(f'The numbers you entered were: {n1} {n2} {n3}')
avg = findaverage(n1, n2, n3)
comparetoavg(n1, n2, n3, avg)
main()
uj5u.com熱心網友回復:
函式introduction,findaverage和comparetoavg是在內部定義的main,但它們永遠不會被呼叫,因此它們中的代碼不會被執行。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/480769.html
