無論有多少值,我都需要創建可以乘以值的東西。我的想法是使用一個串列,然后使用 sum 函式獲取串列的總和,或者獲取串列的第一個值并將其設定為總數,然后將串列的其余部分乘以該總數。有沒有人有辦法做到這一點?
這是我最初的想法:
total = 0
while True:
number = float(input("Enter a number and I’ll keep multiplying until you enter the number 1: "))
if number == 1:
break
else:
total *= number
print(f"The total is: {total}")
但是,正如您可能已經猜到的那樣,它只是自動將其乘以等于 0 的 0。我還希望代碼可以用于減法和除法(已經加法作業)
謝謝!
感謝評論,我發現這樣做的方法是將開始的總數更改為 1,我也發送了錯誤的代碼版本,抱歉,修復后的實際代碼是這樣的:
total = 1
while True:
number = float(input("Enter a number and I’ll keep multiplying until you enter the number 1: "))
if number == 1:
break
else:
total *= number
print(f"The total is: {total}")
我仍然無法完成我查找的花哨的代碼塊,不幸的是它沒有作業
uj5u.com熱心網友回復:
因為你是乘法,所以你必須從 1 開始,因為任何乘以 0 都是 0。我知道為什么你需要 sum()
total = 1
while True:
number = float(input("Enter a number and I’ll keep multiplying until you enter the number 1: "))
if number == 1:
print(f"The total is: {total}")
break
else:
total *= number
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/473784.html