Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python循环添加_Python_Loops_While Loop_Python 3.4_Addition - Fatal编程技术网

Python循环添加

Python循环添加,python,loops,while-loop,python-3.4,addition,Python,Loops,While Loop,Python 3.4,Addition,我是Python和编程的初学者,所以我只懂有限的术语……我在添加while循环时遇到了问题。除了我的第一个MoreExp,它添加了所有内容。几个小时来我一直想弄明白,所以我真的失去了耐心。如果有人能解释为什么这不会添加我所有的MoreExp,我将不胜感激 #Loop to determine expenses while MoreExp != "0": MoreExp = input("Enter more expenses. If no more, enter '0': ")

我是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':  "))

这似乎不是您的全部代码片段。问你月收入和第一笔费用的行在哪里?如果让我猜猜,我假设在进入
while MoreExp
循环之前,您没有将
TotalExp
设置为第一组费用的值。实际上,我在开始循环之前犯了一个错误,我应该在我的原始帖子中发布这个错误。但我也试过你的代码,它成功了!谢谢:)@AlyssaDettmer哈哈,很高兴我帮了你:),下次,把全部代码都发出去;)