Python 循环添加

Python Loops-Adding

我是 Python 和一般编程的初学者,所以我只了解有限数量的术语...我在添加 while 循环时遇到问题。它添加了除我的第一个 MoreExp 以外的所有内容。几个小时以来,我一直在努力弄明白,所以我真的失去了耐心。如果有人可以解释为什么这不会添加我的所有 MoreExp,将不胜感激!

#Loop to determine expenses
while MoreExp != "0":
    MoreExp = input("Enter more expenses.  If no more, enter '0':  ")
    TotalExp += int(MoreExp)
if MoreExp is "0":
   AmountLeft = int(TotalIncome) - int(TotalExp)

TotalExp = int(TotalExp) + int(Expenses)
AmountLeft = int(TotalIncome) - int(TotalExp)

#Output total spent and amount leftover
print("Total amount spent from income: $", TotalExp)
print("Total amount left over after expenses: $", AmountLeft)

所以我的结果是这样的:

What is your monthly income?  100
Enter your expenses:  5
Enter more expenses.  If no more, enter '0':  10
Enter more expenses.  If no more, enter '0':  6
Enter more expenses.  If no more, enter '0':  0
Total amount spent from income: $ 11
Total amount left over after expenses: $ 89

我刚刚弄明白了!!我在循环开始之前不小心向用户询问了 MoreExp,因此它没有将其添加到我的 TotalExp 中。感谢那些帮助过的人!抱歉造成混淆..

抱歉,我重写了您的整个代码,但现在可以使用了!

Tot=input("Enter Income:")
NetInc=Tot
NetXpense=0
c = 0
while (c==0):
    Xpense=input("Enter your expenses:")
    NetXpense+=int(Xpense)
    if (int(Xpense)==0):
        c=1
NetInc = int(Tot)-NetXpense
print("Expenditure:",NetXpense)
print("Net Income:",NetInc)

试试这个让我知道:)

这样试试

MoreExp = int(input("Enter more expenses.  If no more, enter '0':  "))