Python循环加载

2024-10-02 16:21:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我是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)

我得到的结果是:

^{pr2}$

我刚想好了!!我不小心在循环开始之前向用户请求MoreExp,所以它没有将它添加到TotalExp中。感谢那些帮助过我的人!!很抱歉给你添麻烦了。。在


Tags: more编程amountinttotal术语print初学者
2条回答

试试这样

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

对不起,我把你的全部代码都重做了,但现在可以用了!在

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)

试试这个让我知道:)

相关问题 更多 >