Python中的初学者预算程序错误

时间:2022-03-03 10:16:49

I am in the process of creating a basic budget program in Python 3.

我正在使用Python 3创建基本预算程序。

It needs to create a series of three people and give them a salary, 2 expenses, and their left over cash at the end of the week. You will also be comparing their left over expenses to an amount of money they need to save every week. I believe I have it all finished except for this problem whenever I run the program, I get the following message:

它需要创建一个由三个人组成的系列,并在本周末为他们提供工资,2笔费用和剩余现金。您还将比较他们的剩余费用与他们每周需要节省的金额。我相信我已经完成了除了这个问题,每当我运行程序时,我得到以下消息:

Traceback (most recent call last):
  File "/Users/----/Desktop/BudgetProgramTest.py", line 42, in 
    personOneLeftOver = personOneSalary - personOneExpenses
TypeError: unsupported operand type(s) for -: 'str' and 'tuple'

How can I solve this error? What would be the correct way to code this? It would be easier to understand with the original code for my program:

我该如何解决这个错误?编码的正确方法是什么?使用我的程序的原始代码更容易理解:

<prev>
# Ask for each person's name

personOne = input("What is your name?")
personTwo = input("What is your name?")
personThree = input("What is your name?")

# Ask for each person's weekly salary

personOneSalary = input("What is your weekly salary?")
personTwoSalary = input("What is your weekly salary?")
personThreeSalary = input("What is your weekly salary?")

# Ask for personOne's expenditures

personOneGas = input("How much do you spend on gas per week?")
personOneGroc = input("How much do you spend on groceries per week?")
personOneFast = input("How much do you spend on fast food per week?")
personOneHob = input("How much do you spend on hobbies per week?")

personOneExpenses = personOneGas, personOneGroc, personOneFast, personOneHob

# Ask for personTwo's expenditures

personTwoGas = input("How much do you spend on gas per week?")
personTwoGroc = input("How much do you spend on groceries per week?")
personTwoFast = input("How much do you spend on fast food per week?")
personTwoHob = input("How much do you spend on hobbies per week?")

personTwoExpenses = personTwoGas, personTwoGroc, personTwoFast, personTwoHob

# Ask for personThree's expenditures

personThreeGas = input("How much do you spend on gas per week?")
personThreeGroc = input("How much do you spend on groceries per week?")
personThreeFast = input("How much do you spend on fast food per week?")
personThreeHob = input("How much do you spend on hobbies per week?")

personThreeExpenses = personThreeGas, personThreeGroc, personThreeFast, personThreeHob

# Set each person's left over money by subracting expenses from each person's weekly salary

personOneLeftOver = personOneSalary - personOneExpenses
personTwoLeftOver = personTwoSalary - personTwoExpenses
personThreeLeftOver = personThreeSalary - personTwoExpenses

# Set each person's goal to save each week (to compare in the future)

personOneSavesGoal = personOneLeftOver * .10 
peraonTwoSavesGoal = personTwoLeftOver * .10
personThreeSavesGoal = personThreeLeftOver * 10

# See if personOne has met their goal of saving 10% after expenses 

if PersonOneLeftOver >= personOneSavesGoal:
    print("Congratulations, you are on your way to financial freedom!")
else:
    print(personOne, " needs to save more money.")


# See if personTwo has met their goal of saving 10% after expenses 

if PersonTwoLeftOver >= personTwoSavesGoal:
    print("Congratulations, you are on your way to financial freedom!")
else:
    print(personTwo, " needs to save more money.")


# See if personThree has met their goal of saving 10% after expenses 

if PersonThreeLeftOver >= personThreeSavesGoal:
    print("Congratulations, you are on your way to financial freedom!")
else:
    print(personThree, " needs to save more money.")


# Display each person's name, weekly salary, expensive, weekly salary after expenses, and amount of moneuy needed to save

print(personOne, "your weekly salary is: ", personOneSalary, " and your expenses are: ", personOneExpenses, ".")
print("Your weekly salary is ", personOneLeftOver, " after your expenses. You need to save", personOneSavesGoal, ".")


print(personTwo, "your weekly salary is: ", personTwosalary, " and your expenses are: ", personTwoExpenses, ".")
print("Your weekly salary is ", personTwoLeftOver, " after your expenses. You need to save", personTwoSavesGoal, ".")


print(personThree, "your weekly salary is: ", personThreeSalary, " and your expenses are: ", personThreeExpenses, ".")
print("Your weekly salary is ", personThreeLeftOver, " after your expenses. You need to save", personThreeSavesGoal, ".")

<prev>

2 个解决方案

#1


0  

There are some errors on your code.

您的代码有一些错误。

1 - input always return a string, therefore all the places which you need a number you must update to int(input(...)); Eg:

1 - 输入总是返回一个字符串,因此你需要一个数字的所有地方你必须更新为int(input(...));例如:

personTwoGas = int(input("How much do you spend on gas per week?"))

2 - Whenever you sum a tuple or a list, you must use the sum(). Eg:

2 - 无论何时对元组或列表求和,都必须使用和()。例如:

personOneLeftOver = float(personOneSalary) - sum(personOneExpenses)

Most likely there are more errors, just pointed those which might stop you from developing this task.

最有可能出现更多错误,只是指出可能阻止您开发此任务的错误。

#2


0  

Try this:

personOneLeftOver = float(personOneSalary) - sum(personOneExpenses)

You have defined the expenses as a tuple. You can all of the values in the tuple together with sum().

您已将费用定义为元组。您可以将元组中的所有值与sum()一起使用。

#1


0  

There are some errors on your code.

您的代码有一些错误。

1 - input always return a string, therefore all the places which you need a number you must update to int(input(...)); Eg:

1 - 输入总是返回一个字符串,因此你需要一个数字的所有地方你必须更新为int(input(...));例如:

personTwoGas = int(input("How much do you spend on gas per week?"))

2 - Whenever you sum a tuple or a list, you must use the sum(). Eg:

2 - 无论何时对元组或列表求和,都必须使用和()。例如:

personOneLeftOver = float(personOneSalary) - sum(personOneExpenses)

Most likely there are more errors, just pointed those which might stop you from developing this task.

最有可能出现更多错误,只是指出可能阻止您开发此任务的错误。

#2


0  

Try this:

personOneLeftOver = float(personOneSalary) - sum(personOneExpenses)

You have defined the expenses as a tuple. You can all of the values in the tuple together with sum().

您已将费用定义为元组。您可以将元组中的所有值与sum()一起使用。