編集を編集 - 私がテストした部品の数の計算に誤りをしたようだ:Pythonはリストにファイルを読み込む -
lines = len(file.readlines())
N = lines - 2
のそれは時に仕事をしました私は別のスクリプトでそれをテストしました...
初心者の助けが必要です。私は私が自分で解決することができなかった私のpythonスクリプトに問題があります。スクリプトは、テキストファイルから浮動小数点を読み込み、計算を実行することになっています。番号R[]
に番号を取得できません。
以下は、最初の行(s[0]
)が公称値で、2行目(s[1]
)が公差であり、次の行がいくつかの抵抗です。
3300.0
10.0
3132.0
3348.5
3557.3
3467.4
3212.0
3084.6
3324.0
私は、次のコードを持っている:車輪の再発明する必要はありません
R = []
Ntoolow = Nlow = Nhigh = Ntoohigh = 0.0
lines = 0
def find_mean(q):
tot = 0.0
c = len(q)
for x in range (c):
tot += q[x]
return tot/c
def find_median(q):
c = len(q)
if c%2:
return float(q[int(c/2)])
else:
c /= 2
return (q[int(c)]+q[int(c-1)])/2.0
file_name = input("Please enter the file name: ")
file = open(file_name, "r")
s = file.readlines()
Rnom = float(s[0])
Tol = float(s[1])
keepgoing = True
while keepgoing:
s = file.readline()
if s == "":
keepgoing = False
else:
R.append(float(s))
lines = len(file.readlines())
N = lines - 2
R.sort()
Rmin = R[0]
Rmax = R[N-1]
Rlowerlimit = Rnom - Tol
Rupperlimit = Rnom + Tol
for rn in R:
if rn < Rlowerlimit:
Ntoolow += 1
elif rn < Rnom:
Nlow += 1
elif rn <= Rupperlimit:
Nhigh += 1
else:
Ntoohigh += 1
Ptoolow = 100.0 * Ntoolow/N
Plow = 100.0 * Nlow/N
Phigh = 100.0 * Nhigh/N
Ptoohigh = 100.0 * Ntoohigh/N
Rmean = find_mean(R)
Rmedian = find_median(R)
print("Total number of parts tested: " + str(N))
print("The largest resistor is: " + str(Rmax) + " and the smallest is: " + str(Rmin))
print("The mean is: " + str(Rmean) + " and the median is: " + str(Rmedian))
print("The percentage of resistors that have too low tolerance is: " + str(Ptoolow) + "%")
print("The percentage of resistors that have low tolerance is: " + str(Plow) + "%")
print("The percentage of resistors that have high tolerance is: " + str(Phigh) + "%")
print("The percentage of resistors that have too high tolerance is: " + str(Ptoohigh) + "%")
file.close()
あなたの質問を得るカント... uがしたいですかリストR []に戻り数字を追加しますか? –
何が問題だと思われますか?あなたはエラーを受け取りますか?もしそうなら、どちらですか? – usr2564301