Learning and Looping - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Learning and Looping (/thread-5163.html) |
Learning and Looping - AnjyilLee - Sep-21-2017 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. RE: Learning and Looping - Sagar - Sep-21-2017 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() RE: Learning and Looping - AnjyilLee - Sep-22-2017 (Sep-21-2017, 11:50 AM)Sagar Wrote: 1.Define a function calculateExpense(). Thank you for helping! Unfortunately, when I tested your code, I had some issues. 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.
RE: Learning and Looping - Sagar - Sep-22-2017 (Sep-22-2017, 12:27 AM)AnjyilLee Wrote: Thank you for helping! Unfortunately, when I tested your code, I had some issues. I have tested the code on my side before posting it this thread. I have output as following: 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 ? RE: Learning and Looping - buran - Sep-22-2017 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 errorSo, to sum up: Python2 raw_input - returns str and input - evaluates the user inputPython 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 RE: Learning and Looping - AnjyilLee - Sep-23-2017 (Sep-22-2017, 01:57 PM)buran Wrote: The code provided by Sagar is Python3 code, while AnjuiLee is executing it on Python2. 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 If you or @Sagar has the answer to this, I'd really appreciate it. RE: Learning and Looping - Sagar - Sep-23-2017 @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-expenseHere 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. |