Home > other >  How to create a while loop that adds user inputted integers into a list, then displays that list of
How to create a while loop that adds user inputted integers into a list, then displays that list of

Time:01-09

Basically, the user has to guess the correct "heat", by inputting a number between 1 and 10. The correct heat is 6, and I've already got the while loop working for it, but I also want to have it print the attempted "heats" by continuously appending a list with their inputted "heats" until they correctly guess 6.

Here's the code:

heat = int(input("You can move the heat from 1 to 10. What will you turn the heat up to? "))
heats = []

while heat != 6:
  if heat < 3:
    heat = int(input("Too cold...try again. "))
    heat = int(heat)
    print("Heats used: ", heats.append(heat))
  if heat == 3:
    heat = int(input("Lukewarm...? Try again. "))
    heat = int(heat)
    print("Heats used: ", heats.append(3))
  if heat <= 5:
    heat = int(input("Get it a bit warmer...try again. "))
    heat = int(heat)
    print("Heats used: ", heats.append(heat))
  if heat >= 7:
    heat = int(input("Get it a bit cooler...try again. "))
    heat = int(heat)
    print("Heats used: ", heats.append(heat))
  if heat >= 10:
    heat = int(input("Woah, woah, woah! Too hot, too hot! Turn it down, and try again!! "))
    heat = int(heat)
    print("Heats used: ", heats.append(heat))

The idea was that the list of heats tried would begin empty and then gradually fill, but it isn't working. All it outputs is "Heats used: None" :/ Any help is greatly appreciated.

CodePudding user response:

heat.append(heat) or any value returns None
So, you must first append and then print the list
elif would be a better option as the conditions need not be be checked repeatedly

heat = int(input("You can move the heat from 1 to 10. What will you turn the heat up to? "))
heats = []

while heat != 6:
  if heat < 3:
    heat = int(input("Too cold...try again. "))
    heat = int(heat)
    heats.append(heat)
    print("Heats used: ", heats)
  elif heat == 3:
    heat = int(input("Lukewarm...? Try again. "))
    heat = int(heat)
    heats.append(3)
    print("Heats used: ", heats)
  elif heat <= 5:
    heat = int(input("Get it a bit warmer...try again. "))
    heat = int(heat)
    heats.append(heat)
    print("Heats used: ", heats)
  elif heat >= 7:
    heat = int(input("Get it a bit cooler...try again. "))
    heat = int(heat)
    heats.append(heat)
    print("Heats used: ", heats)
  elif heat >= 10:
    heat = int(input("Woah, woah, woah! Too hot, too hot! Turn it down, and try again!! "))
    heat = int(heat)
    heats.append(heat)
    print("Heats used: ",heats)
  • Related