輸入采用數字,直到輸入一個負數,之前的每個數字都乘以 2,結果被列印并格式化為逗號后的第二個數字。我已經嘗試了以下方法,但是回圈只是無休止地繼續給我 1 個結果,我怎樣才能讓它停止并與下一個輸入相乘?
x = float(input())
while x > 0:
result = x * 2
if x < 0:
print('Negative number!')
print(f'Result: {result:.2f}')
uj5u.com熱心網友回復:
你的代碼有很多問題。這是固定版本:
while True:
x = float(input("Input: "))
if x < 0:
print('Negative number!')
break
else:
result = x * 2
print(f'Result: {result:.2f}')
需要“break”來在第一個壞結果上中斷 while 回圈。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/362958.html
標籤:Python 循环 if 语句 while 循环 乘法