1

I am in the process of creating a basic budget program in 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:

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>
Austin
  • 13
  • 2
  • Type-casting should be done at input, see [How to check if string input is a number?](https://stackoverflow.com/questions/5424716/how-to-check-if-string-input-is-a-number) – jpp Mar 22 '18 at 02:42

2 Answers2

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().

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
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:

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:

personOneLeftOver = float(personOneSalary) - sum(personOneExpenses)

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

Adriano Martins
  • 1,788
  • 1
  • 19
  • 23