Python 我做错什么了吗
我学习python的网站没有很好地解释字典,所以我求助于此。我要做的是找出用户键入的天数内这3个房间温度的平均值(最大值为3)。每天有4个温度超过1天。当我运行此代码时,它不会为3个房间产生任何温度Python 我做错什么了吗,python,python-3.x,file,dictionary,Python,Python 3.x,File,Dictionary,我学习python的网站没有很好地解释字典,所以我求助于此。我要做的是找出用户键入的天数内这3个房间温度的平均值(最大值为3)。每天有4个温度超过1天。当我运行此代码时,它不会为3个房间产生任何温度 hi={} days=int(input('How many days of data do you have? ')) print('Average Temperatures:') f = open('temps1.txt') g= open('temps1.txt') and open('tem
hi={}
days=int(input('How many days of data do you have? '))
print('Average Temperatures:')
f = open('temps1.txt')
g= open('temps1.txt') and open('temps2.txt')
o=open('temps1.txt') and open('temps2.txt') and open('temps3.txt')
if days=='1':
for line in f:
parts = line.strip().split(",")
if parts[0] not in hi:
hi[parts[0]] = []
hi[parts[0]].append(int(val[1]))
elif days=='2':
for line in g:
parts = line.strip().split(",")
if parts[0] not in hi:
hi[parts[0]] = []
hi[parts[0]].append(int(val[1]))
elif days=='3':
for line in o:
parts = line.strip().split(",")
if parts[0] not in hi:
hi[parts[0]] = []
hi[parts[0]].append(int(val[1]))
for k, v in hi.items():
print("Room: {}, Avg: {}".format(k, sum(v)/days*4))
temps1.txt仅在用户在输入问题中键入1时激活:
Living Room,23
Bedroom,24
Kitchen,22
Living Room,24
Bedroom,26
Kitchen,22
Living Room,25
Bedroom,26
Kitchen,23
Living Room,24
Bedroom,26
Kitchen,23
temps2.txt仅在用户在第一个问题中输入2时激活(与temps1.txt一起使用):
temps3.txt仅在用户在第一个问题中输入3时激活(与temps1.txt和temps2.txt一起使用):
我是否能够生成一个代码来实现这一点,但没有类似的导入?还有一件事,我是否能够将变量分配给“for in”语句和“if”语句?让我们分割数据文件的创建和处理,从而得出以下答案:
def createData():
"""Create demo data as files 'temp1.txt','temp2.txt','temp3.txt'"""
with open("temp1.txt","w") as f:
f.write("""Living Room,23
Bedroom,24\nKitchen,22\nLiving Room,24\nBedroom,26\nKitchen,22\nLiving Room,25\nBedroom,26\nKitchen,23\nLiving Room,24\nBedroom,26\nKitchen,23\n""")
with open("temp2.txt","w") as f:
f.write("""Living Room,26\nBedroom,24\nKitchen,23\nLiving Room,27\nBedroom,24\nKitchen,24\nLiving Room,28\nBedroom,25\nKitchen,26\nLiving Room,29\nBedroom,26\nKitchen,28\n""")
with open("temp3.txt","w") as f:
f.write("""Living Room,28\nBedroom,29\nKitchen,28\nLiving Room,28\nBedroom,30\nKitchen,29\nLiving Room,29\nBedroom,29\nKitchen,31\nLiving Room,21\nBedroom,23\nKitchen,21\n""")
使用此演示数据,我们开始用户输入和计算。我们将1到3个文件中所有需要的数据读入一个dict并输出:
# create all data files
createData()
# process data
nrDays = 0
# ask for how many days, catch invalid values until valid one is given
while nrDays not in range(1,4):
try:
nrDays=input('How many days of data do you have? [1-3] ')
nrDays=int(nrDays)
except: # catchall
print("Parsing error. 1 to 3 allowed. {} given".format(nrDays))
# read data only from files needed
temps = {}
for d in range(1,nrDays+1):
# format file names as temp1.txt to temp3.txt as needed
with open ("temp{}.txt".format(d),"r") as f: # open file
for line in f:
if line.strip(): # if line not empty, f.e. dangling newline at end
room,temp = line.strip().split(",")
temp = int(temp) # convert to int
k = temps.setdefault(room,[]) # creates the empty list if needed
# returns the value if present
k.append(temp)
print('Average Temperatures:')
for k, v in temps.items():
print("Room: {}, Avg: {}".format(k, sum(v)/len(v))) # get the length from the list
输出:
How many days of data do you have? 3
Average Temperatures:
Room: Living Room, Avg: 26.0
Room: Bedroom, Avg: 26.0
Room: Kitchen, Avg: 25.0
How many days of data do you have? 2
Average Temperatures:
Room: Living Room, Avg: 25.75
Room: Bedroom, Avg: 25.125
Room: Kitchen, Avg: 23.875
How many days of data do you have? 1
Average Temperatures:
Room: Living Room, Avg: 24.0
Room: Bedroom, Avg: 25.5
Room: Kitchen, Avg: 22.5
如果您输入1-3之外的任何内容,您将得到一个新的输入提示(如果输入了int),或者一条消息和一个新的提示(如果没有给出int)。
open(…)
返回一个文件的句柄。通过和组合多个文件句柄,您希望实现什么?Doku:您将days
转换为整数,但如果days=='1',
则尝试将其作为字符串进行比较。这行不通。顺便说一句,g=open('temps1.txt')和open('temps2.txt')
并不像你想象的那样。
# create all data files
createData()
# process data
nrDays = 0
# ask for how many days, catch invalid values until valid one is given
while nrDays not in range(1,4):
try:
nrDays=input('How many days of data do you have? [1-3] ')
nrDays=int(nrDays)
except: # catchall
print("Parsing error. 1 to 3 allowed. {} given".format(nrDays))
# read data only from files needed
temps = {}
for d in range(1,nrDays+1):
# format file names as temp1.txt to temp3.txt as needed
with open ("temp{}.txt".format(d),"r") as f: # open file
for line in f:
if line.strip(): # if line not empty, f.e. dangling newline at end
room,temp = line.strip().split(",")
temp = int(temp) # convert to int
k = temps.setdefault(room,[]) # creates the empty list if needed
# returns the value if present
k.append(temp)
print('Average Temperatures:')
for k, v in temps.items():
print("Room: {}, Avg: {}".format(k, sum(v)/len(v))) # get the length from the list
How many days of data do you have? 3
Average Temperatures:
Room: Living Room, Avg: 26.0
Room: Bedroom, Avg: 26.0
Room: Kitchen, Avg: 25.0
How many days of data do you have? 2
Average Temperatures:
Room: Living Room, Avg: 25.75
Room: Bedroom, Avg: 25.125
Room: Kitchen, Avg: 23.875
How many days of data do you have? 1
Average Temperatures:
Room: Living Room, Avg: 24.0
Room: Bedroom, Avg: 25.5
Room: Kitchen, Avg: 22.5