使用“if-elif”流控制和“while”回圈撰寫一個 Python 代碼片段,它將:
- 指示用戶輸入一個大于 0 且小于或等于 10 的數字,并將輸入作為浮點值存盤在變數中
- 如果輸入的數字大于0且小于等于10,
- 使用“while”回圈將數字加到自身上,直到總和超過 100 的值。
- 總和超過 100 后,使用列印陳述句輸出總和
- 否則,輸出訊息“您沒有輸入 0 到 10 之間的值”
我的答案 :
inval = float(input('Input a number greater than zero and less than or equal to 10: '))
if inval > 0 and inval <= 10:
while inval < 100:
inval = inval
continue
else:
print(inval)
elif inval <= 0 or inval > 10:
print('You did not enter a value between 0 and 10')
uj5u.com熱心網友回復:
您的答案不可接受的原因是代碼應該回傳100.0輸入,例如6.25. 但是,200.0由于您的 while 條件,您的代碼回傳。您只需添加一個等號 (=) 即可解決此問題。而且,我從您的代碼中洗掉了不必要的部分。
inval = float(input('Input a number greater than zero and less than or equal to 10: '))
if inval > 0 and inval <= 10:
while inval <= 100:
inval = inval
print(inval)
else:
print('You did not enter a value between 0 and 10')
uj5u.com熱心網友回復:
我認為唯一可能出錯的是 While 回圈的條件(你應該把它inval <= 100)。但是,問題可能出在回圈內的 else 上;因為,它不是“有效的”,在 inval 值大于 100 后,即使 else 不存在,它也會列印該值。看:
inval = float(input('Input a number greater than zero and less than or equal to 10: '))
if inval > 0 and inval <= 10:
while inval <= 100:
inval = inval
print(inval)
elif inval <= 0 or inval > 10:
print('You did not enter a value between 0 and 10')
我在您的代碼中沒有看到其他問題:)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/368601.html
標籤:Python 蟒蛇-3.x python-2.7 蟒蛇请求
