Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Learning and Looping
#1
Posting this is both for figuring out my homework and to try learning the site's preferred way for me to format my questions. I've looked over the help posts on this topic, but I will only how learn how to format these questions properly by doing so please feel free to help me with this as well as with the problem.

For this program, I need to prompt the user for their monthly budget then ask for each expense (storing each) before finding out how much money is left from the budget.

def main():
    float(raw_input("Enter monthly budget: "))
    more = 'y'

    float(raw_input("Enter monthly expense: "))

    if more == 'y':
        float(raw_input("Enter monthly expense: "))

    else:
        

main()
If what I have is correct so far (which it very well might not be), all I need is to subtract the expenses from the budget, but I am not quite sure how to do so. Any help (both on the programming and how to post questions properly) would be appreciated.
Reply
#2
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()
Reply
#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
#4
(Sep-22-2017, 12:27 AM)AnjyilLee Wrote: 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 have tested the code on my side before posting it this thread. I have output as following:

Output:
Enter your monthly budget 13123 Enter your monthly expense 2332 Money left in your budget is: 10791.0 Want to do it more ? y Enter your monthly budget 45343 Enter your monthly expense 2343 Money left in your budget is: 43000.0 Want to do it more ?
Please verify whether you are copying and pasting it correctly, keeping the indentation.

Quote: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.
Could you please elaborate what you are not understanding in this code ?
Reply
#5
The code provided by Sagar is Python3 code, while AnjuiLee is executing it on Python2.
In Python3 input is same as raw_input in Python2..
In Python2 input will evaluate the user input and because there is no name y in the code it raise the NameError- that is the cause of the above error

So, to sum up:
Python2
raw_input - returns str and input - evaluates the user input
Python 3
input returns str, like raw_input on Python2, while eval(input()) is same as input in python2

@Sagar - I think OP has problem to understand the traceback, not the code
Reply
#6
(Sep-22-2017, 01:57 PM)buran Wrote: The code provided by Sagar is Python3 code, while AnjuiLee is executing it on Python2.
In Python3 input is same as raw_input in Python2..
In Python2 input will evaluate the user input and because there is no name y in the code it raise the NameError- that is the cause of the above error

So, to sum up:
Python2
raw_input - returns str and input - evaluates the user input
Python 3
input returns str, like raw_input on Python2, while eval(input()) is same as input in python2

@Sagar - I think OP has problem to understand the traceback, not the code

Thank you, Buran! I'll have to remember to include my use of Python 2 for now on. I fixed the issue and it works now, but after prompting to continue, it asks to enter the budget again, when I need it to keep the last budget I put in and just let me add another expense to subtract from the budget each time. I feel like this would just need a little fix and that I've done it before, but for the life of me, I can't remember Undecided

If you or @Sagar has the answer to this, I'd really appreciate it.
Reply
#7
@buran your answer reflects the experience you have got in Python and I appreciate it a lot.

I learned a lot from this forum.

@AnjyilLee if you want to keep the previous budget, you have to move the following:

budget = float(input("Enter your monthly budget"))
out of the calculateExpense() function

I have updated my code for Python 2+
Hope this will not create any issue.
def calculateExpense(moneyLeft):  
  expense = float(input("Enter your monthly expense"))
  moneyLeft = moneyLeft-expense
  print("Money left in your budget is: " + str(moneyLeft))
  more = raw_input("Want to do it more ?").lower()
  if(more == "yes" or more== "y"):
    calculateExpense(moneyLeft)
  else:
    return
   
budget = float(input("Enter your monthly budget"))
calculateExpense(budget)
1. I am asking user to give his budget outside the function calculateExpense()
2. I am passing that budget to calculateExpense().
3. In the function calculateExpense, you will find that value in moneyLeft.
4. Then I am asking user for expense and calculating the moneyLeft by
moneyLeft = moneyLeft-expense
Here I am subtracting expense from moneyLeft and assigning that value to the moneyLeft variable
itself.
5. Inside the if condition which checks whether the user wants it more, I am passing the new moneyLeft into the calculateExpense().

I tried my best to explain myself, but still if you have any issue let me know.
And you need to practice a lot.
Thanks.
Reply


Forum Jump:

User Panel Messages

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