Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Learning and Looping
#3
(Sep-21-2017, 11:50 AM)Sagar Wrote: 1.Define a function calculateExpense().
2.In that function, you have to input the monthly budget and store it in budget variable.
3.Input the monthly expense and store it in expense variable.
4.Calculate money left.
5.Print the calculated money left.
6.Ask user if he wants to calculate more and store the answer in more variable with its case lowered.
7.Check whether the answer is "y" or "yes". If it is then call the calculateExpense() function. This is called recursion.
8. If the answer is something else then return.
9. After that function definition, call the function calculateExpense()

You can always modify and scale up the code and add new features into it.
I am sure it will help.
def calculateExpense():
  budget = float(input("Enter your monthly budget"))
  expense = float(input("Enter your monthly expense"))
  moneyleft = budget-expense
  print("Money left in your budget is: ", moneyleft)
  more = input("Want to do it more ?").lower()
  if(more == "yes" or more== "y"):
    calculateExpense()
  else:
    return
  
calculateExpense()

Thank you for helping! Unfortunately, when I tested your code, I had some issues.

Error:
Traceback (most recent call last): File "C:\Users\Sami3_000\Documents\School\Intro to Program Logic and Design\Ch4Ex3.py", line 18, in <module> calculateExpense() File "C:\Users\Sami3_000\Documents\School\Intro to Program Logic and Design\Ch4Ex3.py", line 12, in calculateExpense more = input("Want to do it more ?").lower() File "<string>", line 1, in <module> NameError: name 'y' is not defined
I've looked through to see if I can make sense of what it's talking about, but sadly I cannot. If you can help with this too, I'd really appreciate it.
Reply


Messages In This Thread
Learning and Looping - by AnjyilLee - Sep-21-2017, 04:47 AM
RE: Learning and Looping - by Sagar - Sep-21-2017, 11:50 AM
RE: Learning and Looping - by AnjyilLee - Sep-22-2017, 12:27 AM
RE: Learning and Looping - by Sagar - Sep-22-2017, 08:08 AM
RE: Learning and Looping - by buran - Sep-22-2017, 01:57 PM
RE: Learning and Looping - by AnjyilLee - Sep-23-2017, 01:02 AM
RE: Learning and Looping - by Sagar - Sep-23-2017, 07:43 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020