![](/img/trans.png)
[英]Use a while loop to keep count of even integers that a user inputs into a list?
[英]How to create a while loop that adds user inputted integers into a list, then displays that list of user inputs after every failed input?
基本上,用戶必須通過輸入 1 到 10 之間的數字來猜測正確的“熱量”。正確的熱量是 6,我已經讓 while 循環為它工作,但我也想讓它打印通過不斷地在列表中添加他們輸入的“預賽”來嘗試“預賽”,直到他們正確猜出 6。
這是代碼:
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))
當時的想法是,嘗試的預賽列表將開始為空,然后逐漸填滿,但它不起作用。 它輸出的所有內容都是“使用的熱量:無”:/非常感謝任何幫助。
heat.append(heat)
或任何值返回 None
所以,你必須先 append 然后打印列表
elif
將是更好的選擇,因為不需要重復檢查條件
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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.