Python 如何要求用户输入数字,然后存储在文本文件中?
我需要让用户输入一定数量的数字5,并将它们存储在文本文件中。 我还需要找到这些数字的平均值。我会使用求和函数,然后除以5吗 这是针对python的,我在OSX上使用IDLE 3.4版本Python 如何要求用户输入数字,然后存储在文本文件中?,python,text,input,numbers,average,Python,Text,Input,Numbers,Average,我需要让用户输入一定数量的数字5,并将它们存储在文本文件中。 我还需要找到这些数字的平均值。我会使用求和函数,然后除以5吗 这是针对python的,我在OSX上使用IDLE 3.4版本 lst = list() #creates a list to later store nubers in def addtolist(): #creates a function for adding numbers to the list def listcount(): # try
lst = list() #creates a list to later store nubers in
def addtolist(): #creates a function for adding numbers to the list
def listcount(): #
try:
lst.append(int(input("Enter a number:")))
except ValueError:
print("That was not a number")
print(lst)
listcount()
yes = ["yes", "y"]
no = ["no", "n"]
yup = ["yes", "y"]
nah = ["no", "n"]
enteragain = input("Enter another number? yes/no")
if enteragain in yes:
print("Ok")
elif enteragain in no:
print("We'll try again")
addtolist()
runagain = input("Are those the numbers you wanted to enter?")
if runagain in yes:
print("Ok")
elif runagain in no:
print("We'll try again")
addtolist()
addtolist()
我对它做了一些修改,并没有将列表存储为文本文件,而是决定将其保存为列表形式。以下是我在最后得出的结论:
lst = list()
def addtolist(): #creates a function for adding numbers to the list
def listcount(): #
try:
lst.append(int(input("Enter a number: ")))
except ValueError:
print("That was not a number")
addtolist()
print(lst)
listcount()
yes = ["yes", "y"]
no = ["no", "n"]
def playagain():
enteragain = input("Enter another number? yes/no?... ")
if enteragain in no:
print("Ok!\n Calculating average.../n")
elif enteragain in yes:
print("Ok then!")
addtolist()
else:
print("Please type either yes or no")
playagain()
playagain()
addtolist()
total = sum(lst)
numbers = len(lst)
average = total/numbers
print("The average of", lst, "is", average)
先写一些代码。你的问题一点也不难,最好的部分是你已经知道了解决方案。欢迎使用堆栈溢出!所有已发布的内容都是程序说明。然而,我们需要你去。我们不能确定你想从我们这里得到什么。请在您的帖子中加入我们可以回答的有效问题。提醒:确保你知道,要求我们为你编写程序和建议是离题的。@KevinGuan我想我们可以非常确定他们想从我们这里得到什么…@jonrsharpe这不是重要的部分:P.因为我使用,在评论列表中,这似乎是这篇文章的正确答案。我是否可以使用sum函数,然后除以5你试过了吗?怎么搞的?问题是您不确定如何计算平均值,如何在Python中实现平均值,您尝试过实现平均值,但它不起作用。。。?