我正在看这个网站上的其他python代码,它们也有类似的问题,但是这些代码与我的代码有很大不同,并且看起来更加混乱,因此阅读它们并没有太大帮助。无论如何,我想看看如何让用户在他们创建的列表中添加或删除,该如何做?这段代码中的所有内容都可以正常运行,但是我只想知道我需要添加哪些内容,以便我可以让用户添加或删除项目。
temperatureList = list()
weather=int(input("Enter the amount of days that you are looking at for
the weather:"))
print("Enter the high temperature for those next days: ")
for i in range(int(weather)):
k=int(input(""))
temperatureList.append(int(k))
sm=sum(temperatureList)
avg=sm/weather
print("SUM = ",sm)
print("AVERAGE = ",avg)
答案 0 :(得分:0)
对于添加和删除,您可以有一个循环,不断询问用户添加和删除数字,并显示更新的统计信息 例如
while True:
action = int(input("Input -1 if you want to add to the list again. Or -2 if you want to remove from the list"))
if action == -1:
print("Enter what you want to be added ")
add = int(input(""))
temperatureList.append(add)
print(temperatureList)
sm = sum(temperatureList)
avg = sm / weather
print("SUM = ", sm)
print("AVERAGE = ", avg)
elif action == -2:
if len(temperatureList) > 1:
del temperatureList[-1]
print(temperatureList)
sm = sum(temperatureList)
avg = sm / weather
print("SUM = ", sm)
print("AVERAGE = ", avg)
else:
print("Everything removed from the list")