Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.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_Python 3.x - Fatal编程技术网

Python预算代码问题

Python预算代码问题,python,python-3.x,Python,Python 3.x,我对以下代码有问题。当用户选择N时,我会收到一条错误消息: budget = input("please enter the budget amount for given month\n ") sum_ = 0 expense = 0 i = 0 print("Please enter the expenses for the given month:") while (1): expense1 = input("Please enter expense number " + str(

我对以下代码有问题。当用户选择N时,我会收到一条错误消息:

budget = input("please enter the budget amount for given month\n ")
sum_ = 0
expense = 0
i = 0

print("Please enter the expenses for the given month:")
while (1):
   expense1 = input("Please enter expense number " + str(i + 1) + " \n")
   sum_ = sum_ + int(expense)
   c = input("If you want to enter more expense press y else n \n")
   if c == 'y':
      i += 1
      continue
   else:
      break
if sum_ > budget:
   print("You went over budget")
else:
   print("You are under budget")

两段代码给您一个提示:

budget = input("please enter the budget amount for given month\n ")

expense1 = input("Please enter expense number " + str(i + 1) + " \n")
sum_ = sum_ + int(expense)
注意到一些不同吗


另外,不要使用str(i+1)<代码>“请废话废话”{0}废话\n。格式(i+1)

错误1。当我用Python3运行它时,我得到

If you want to enter more expense press y else n 
n
Traceback (most recent call last):
  File "C:\Programs\python34\tem.py", line 16, in <module>
    if sum_ > budget:
TypeError: unorderable types: int() > str()
如果要输入更多费用,请按y或n
N
回溯(最近一次呼叫最后一次):
文件“C:\Programs\python34\tem.py”,第16行,在
如果金额>预算:
TypeError:无序类型:int()>str()
您应该查看错误消息。它说,
sum
是一个整数,
budget
是一个字符串。显而易见的解决办法是将
budget
int(budget)
一起设置为int。如果你真的不明白这一点,你可能应该看更多的教程。无论如何,你应该按照凯文的建议把它贴出来

错误2。您已将费用初始化为0。这掩盖了调用输入的费用
expense1
的错误。因此,您只需将
int(0)
添加到最初的0
sum\uu
。解决方法是放弃初始化并更正拼写错误。您还应在末尾打印总和,以便检查其是否正确


小号3。必须说是/否才能输入另一项费用,这真的很烦人。当用户没有输入任何内容时停止。

那么,错误消息是什么?另外,请修正你的缩进。