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

时间:2018-03-22 02:33:38

标签: python python-3.x

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

它需要创建一个由三个人组成的系列,并在本周末为他们提供工资,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'

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

<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 个答案:

答案 0 :(得分:0)

试试这个:

personOneLeftOver = float(personOneSalary) - sum(personOneExpenses)

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

答案 1 :(得分:0)

您的代码存在一些错误。

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

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

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

personOneLeftOver = float(personOneSalary) - sum(personOneExpenses)

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