簡體   English   中英

是否有提示用戶輸入特定數字的代碼?

[英]Is there a code where it prompts the user to enter specific numbers?

我正在練習 Python,但我的代碼存在一些問題。 我正在嘗試定義主 function 並提示用戶輸入三個特定數字。 該程序應該找到這三個數字的平均值並將平均值與輸入的數字進行比較,然后計算有多少數字等於平均值。

這是我的代碼:

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()
  1. 我的代碼go不會通過。 它只打印 3 次“Please Enter Number”並打印對帳單。
  2. 我希望我的代碼迭代 10 次。 我知道必須使用循環,但我不確定將循環放在哪里。
  3. 我希望 2 次迭代的三個值等於平均值,3 次迭代沒有等於平均值的值,5 次迭代中有一個值等於平均值。 有人可以幫幫我嗎?

您需要調用您的函數以便它們實際運行並確保它們在同一個 scope 中以便它們實際上是可調用的

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()

您的代碼似乎有幾個問題。

  1. 你只調用主要函數。 主 function 中只有 function 定義,但沒有 function 調用。 您必須將 function 調用放在主 function 中,然后再調用主 function。最好,您也可以將 function 定義放在主 function 定義之上。

  2. 您可以使用 input() function 來請求用戶輸入。 請參閱下面的示例。

  3. 如果要調用這些函數 10 次,則必須將它們的 function 調用放在具有 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 循環、輸入 function 以及如何正確定義和調用方法的示例。 您可能需要根據您的需要進一步調整它。

函數introductionfindaveragecomparetoavg定義在main中,但它們從未被調用,因此它們中的代碼不會被執行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM