Home > Net >  Functions not being called correctly
Functions not being called correctly

Time:11-30

This is a code that prompts the user for the amount of months they want to budget analyze, prompts for the budget the user has, prompts for how much the user spent that month, and then calculates if the user is over or under their budget. When code is run, it prompts user one twice, and then creates errors:

 Traceback (most recent call last):
  File "C:\Users\\Desktop\", line 53, in <module>
    main()
  File "C:\Users\\Desktop\", line 51, in main
    AnalyzeBudget(months)
  File "C:\Users\\Desktop\", line 46, in AnalyzeBudget
    MoBudget,MoSpent = GetMonthBudgetandSpent(month)
  File "C:\Users\\Desktop\", line 40, in GetMonthBudgetandSpent
    return int(Mobudget, MoSpent)
TypeError: 'str' object cannot be interpreted as an integer

any help is appreciated.

def DescribeProgram():
 
   print("""\
This program uses a for loop to monitor your budget.
The program will prompt you to enter your budget, and amount spent
for a certain month and calculate if your were under or over budget.
You will have the option of choosing how many months you would like to
monitor.\n""")


def GetMonths():
    Months = input("Enter the number of months you want to analyze")
    return int(Months)

def GetMonthBudgetandSpent(month):
      Mobudget = input("Enter the budget you have for the month")
      MoSpent = input("Enter the amount you spent this month")
      return int(Mobudget, MoSpent)

def AnalyzeBudget(months):
    for month in range(1,months 1):
      print("\nMonth",month,":")
      print("=======")
      MoBudget,MoSpent = GetMonthBudgetandSpent(month)

def main():
 DescribeProgram()
 months = GetMonths()
 AnalyzeBudget(months)

main()

CodePudding user response:

You can change the return type of GetMonths() :

def GetMonths():
    Months = input("Enter the number of months you want to analyze")
    return int(Months)

Or You can cast the type to int in range() of AnalyzeBudget()

def AnalyzeBudget(months):
    for month in range(1,int(months) 1):
      print("\nMonth",month,":")
      print("=======")
      MoBudget,MoSpent = GetMonthBudgetandSpent(month)

CodePudding user response:

As @quamrana said you need to

def GetMonths():
    Months = input("Enter the number of months you want to analyze")
    return int(Months)

The error says TypeError: can only concatenate str (not "int") to str because when you ask for Months in

Months = input("Enter the number of months you want to analyze")

Python read the input as a string, then in months 1 you are adding a string with an int so you get the error. I recommend to factorize your code in an class where each function is a method, I can help you with this if you need to.

Edit(This is not related to the original post). You have an error in GetMonthBudgetandSpent, you can do the following.

def GetMonthBudgetandSpent(month):
  Mobudget = input("Enter the budget you have for the month")
  MoSpent = input("Enter the amount you spent this month")
  MoSpent = int(MoSpent)
  Mobudget = int(Mobudget)
  return Mobudget, MoSpent

As @Pranav Hosangadi said int function int docs takes in the first input of the thing you want to convert, so you can only pass one string.

For the if statement you asked for in the comments.

def AnalyzeBudget(months):
for month in range(1,months 1):
  print("\nMonth",month,":")
  print("=======")
  MoBudget,MoSpent = GetMonthBudgetandSpent(month)
  if MoBudget >= MoSpent:
    print("Safe zone")
  else :
    print("Danger zone") 

Here if MoBudget >= MoSpent checks if the budget is more than spent in that month, if so then print "safe zone", else (MoSpent > Mobudget) then print "Danger zone"

  • Related