从我存储的游戏得分的文本文件中找到范围,最小值,最大值和平均值之后,我想从这些得分中找到标准偏差,但是我不确定该如何进行。
这是我到目前为止所拥有的:
file = open('stats.txt', 'a+')
file.write('\n' + 'Score: ' + str(1))
file.close()
numbers = []
with open('stats.txt') as fh:
count = 0
for line in fh:
count += 1
numbers.append(float(line.split()[1]))
file = open('maths.txt', 'w+')
file.write('Average Score: ' + str(sum(numbers)/len(numbers)) + "\n")
file.write('Maximum Score: ' + str(max(numbers)) + "\n")
file.write('Minimum Score: ' + str(min(numbers)) + "\n")
maxn = max(numbers)
minn = min(numbers)
rangex = (maxn) - (minn)
file.write('Range of Scores: ' + str(rangex))
file.close()
我的文本文件是什么样的:
Score: 3
Score: 0
Score: 13
Score: 13
Score: 9
Score: 0
Score: 0
Score: 0
Score: 0
Score: 0
Score: 0
Score: 31
Score: 0
Score: 0
Score: 0
Score: 0
Score: -8
Score: 0
Score: 0
感谢您的帮助
答案 0 :(得分:0)
您只需要使用numpy的标准偏差函数即可。
添加到代码的开头:
supplied_username = input("Please enter your name. ")
print("Your username has been created and is", supplied_username)
supplied_password = input("Now please create a password. ")
file = open("Login.txt","a")
file.write (supplied_username)
file.write (",")
file.write (supplied_password)
file.write("\n")
file.close()
logged_in = False
with open('Login.txt', 'r') as file:
for line in file:
supplied_username, supplied_password = line.split(',')
username = input("please enter your username")
if username == supplied_username:
password = input("please enter your password")
if password == supplied_password:
logged_in = True
break
if logged_in:
print("welcome!")
else:
("please, register an account")
然后使用:
import numpy as np
答案 1 :(得分:0)
您可以读取文件并在:
上拆分以创建列表:
l = []
In [400]: with open('stats.txt', 'r') as f:
...: for i in f:
...: l.append(int(i.split(':')[1].strip()))
In [401]: l
Out[401]: [3, 0, 13, 13, 9, 0, 0, 0, 0, 0, 0, 31, 0, 0, 0, 0, -8, 0, 0]
In [403]: import statistics
In [402]: statistics.stdev(l)
Out[402]: 8.357158922932953
numpy
:In [404]: import numpy as np
In [403]: np.std(l)
Out[403]: 8.1342611825629