I have to write a program that accepts a sequence of average daily temperatures and put those temperatures into a list, but I can’t figure out how. What I tried below does not work. Instead of giving me a list it just gives me the last input.
def main():
#create a list to store the temperatures.
tempList = []
while True:
dailyTemp = raw_input(
"Enter average daily temperature or -100 to quit: ")
# assign dailyTemo to tempList list
tempList = [dailyTemp]
print tempList
if dailyTemp == '-100':
break
main()
To append to a list, you have to do
templist.append('thingtoappend').In your case, you’d want something like this:
What the code that you posted does instead is, it says that the temperature that the user entered, is the list – so each time they enter a new temperature, it replaces the last one they entered.