簡體   English   中英

python while 循環打印每一行

[英]python while loop printing each line

user_input = input("Please enter a number: ") user_input = 
int(user_input) total = 0
i = 1
while i<=user_input:
    total += i
    i += 1
    print(total)

    

Output 是:

Please enter a number: 5
1
3
6
10
15

但我希望它只打印最后一個值。 請幫忙。

您可以嘗試從循環外部調用 print function ,然后您將獲得一份打印

user_input = int(input("Please enter a number: "))
total = 0
i = 1
while i<=user_input:
    total += i
    i += 1
print(total)

如果只想打印最后一個值,則需要將 print function 跳出循環

i = 1
total = 0

while i<=user_input:
   total += i
   i += 1
   
print(total)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM