简体   繁体   English

在 while 循环中计算用户的平均值(通过提示输入数据),直到 Python 中的某个条件

[英]Calculating average from user (through prompt input data) in a while loop until some condition in Python

I have written the following code which is working fine for me and I want to write the same code in a function (ie, def function_name()) - which I have tried below as well but that's not a good approach and it's poorly written).我已经编写了以下对我来说工作正常的代码,我想在 function 中编写相同的代码(即 def function_name())——我也在下面尝试过,但这不是一个好方法,而且写得不好) . So, how can I fix the code and how can I put input in an argument of my function name upon invoking/calling argument so that I can get my ultimate answer.那么,如何修复代码以及如何在调用/调用参数时将输入输入到我的 function 名称的参数中,以便获得最终答案。

# Normal code:
from statistics import mean 
lst = list()
while True:
    user_int = input("Enter a number: ")
    if user_int == "done":
        break
    try:
        float_user_int = float(user_int)
    except:
        print("Bad input. Enter a valid number")
        continue 
    lst.append(float_user_int)
print(mean(lst)) 
# Function Method I have tried which is working fine but it's poorly written and not yielding correct output. Rather, I am getting an error i.e., NameError: name 'float_user_input' is not defined. How can I write user_input integer numbers in an argument of my function name?

def avg_list(float_user_input):
    from statistics import mean # import statistics
    lst = list()
    while True:
        user_int = input("Enter a number: ")
        if user_int == "done":
            break
        try:
            float_user_int = float(user_int)
        except:
            print("Bad input. Enter a valid number")
            continue # quit()
        lst.append(float_user_int)
    return(mean(lst)) # statistics.mean()
print(avg_list(float_user_input))

# This is the one I tried and it seems to be working with function but I don't think this code is perfect because I have done nothing in it except copy pasting the entire code under def function statement. How can I put user input number in an argument of function name?

def avg_list():
    from statistics import mean # import statistics
    lst = list()
    while True:
        user_int = input("Enter a number: ")
        if user_int == "done":
            break
        try:
            float_user_int = float(user_int)
        except:
            print("Bad input. Enter a valid number")
            continue # quit()
        lst.append(float_user_int)
    return(mean(lst)) # statistics.mean()
print(avg_list())

Your help would be highly appreciated.您的帮助将不胜感激。 Thanks.谢谢。

Remove float_user_input from function declaration and function call从 function 声明和 function 调用中删除float_user_input

I think you want a function which takes input list from user such as get_input_list and calculate the mean as below:我认为您想要一个 function ,它从用户那里获取输入列表,例如get_input_list并计算如下平均值:

from statistics import mean # import statistics

def avg_list(input_list):
    return(mean(input_list))


def get_input_list():
    # Take user input here
    lst = list()
    while True:
        user_int = input("Enter a number: ")
        if user_int == "done":
            break
        try:
            float_user_int = float(user_int)
        except:
            print("Bad input. Enter a valid number")
            continue
        lst.append(float_user_int)
    return lst

# call function
print(avg_list(get_input_list()))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM