+ 2

Newbie! I want to clean this up

Honestly just started trying to learn python and am having trouble with an exercise. How do I clean this up: [ name1 = input("what is your name? ") name2 = input("what is your name? ") name3 = input("What is your name? ") salary1 = eval(input("What is name1 weekly salary? ") salary2 = eval(input("What is name2 weekly salary? ") salary3 = eval(input("What is name3 weekly salary? ") groceries1 = eval(input("what is weekly grocery amount for name1? ") groceries2 = eval(input("what is weekly grocery amount for name2? ") groceries3 = eval(input("what is weekly grocery amount for name3? ") cable1 = eval(input("what is weekly cable amount for name1? ") cable2 = eval(input("what is weekly cable amount for name2? ") cable3 = eval(input("what is weekly cable amount for name3? ") gas1 = eval(input("what is weekly gas amount for name1? ") gas2 = eval(input("what is weekly gas amount for name2? ") gas3 = eval(input("what is weekly gas amount for name3? ") phone1 = eval(input("what is weekly phone bill amount for name1? ") phone2 = eval(input("what is weekly phone bill amount for name2? ") phone3 = eval(input("what is weekly phone bill amount for name3? ") weekly_expenses1 = groceries1 + cable1 + gas1 + phone1 weekly_expenses2 = groceries2 + cable2 + gas2 + phone2 weekly_expenses3 = groceries3 + cable3 + gas3 + phone3 left_over1 = salary1 - weekly_expenses1 left_over2 = salary2 - weekly_expenses2 left_over3 = salary3 - weekly_expenses3 savings1 = salary1 * .10 savings2 = salary2 * .10 savings3 = salary3 * .10 print("Name:", name1, "weekly salary:", salary1, "expenses:", weekly_expenses1, "salary after expenses:", left_over1, "amount of money to save:" savings1) print("Name:", name2, "weekly salary:", salary2, "expenses:", weekly_expenses2, "salary after expenses:", left_over2, "amount of money to save:" savings2) print("Name:", name3, "weekly salary:", salary3, "expenses:", weekly_expenses3, "salary after expenses:", left_over3, "amount of

4th Oct 2017, 6:40 PM
Rick Wallace
Rick Wallace - avatar
8 Answers
+ 1
you would have to decide how you use the variables depending on your usage scenario. You could use just 1 or 2 variables in your code or you could set up 1 for each expense or a list for all expenses. Either way you would use a loop and/or a function to simplify the code. If you imagine you are designing software for a real company, then there could be several usage scenarios eg 1. collecting expenses monthly from each employee 2. creating expense report for management 3. updating payroll 4. detecting fraud or unusual expense claims etc. each use case may have different requirements and also will be done at different times. This means we separate the ideas of variables and data. variables are used programmatically to manipulate data but they are not the same thing. So long as you record, store and retrieve data reliably you can use any variables you like
6th Oct 2017, 4:54 AM
Ben-Davis
Ben-Davis - avatar
+ 9
It is generally a good idea to use a loop to iterate the entire thing and store data inside arrays instead of having multiple variables to store each element.
5th Oct 2017, 1:04 AM
Hatsy Rei
Hatsy Rei - avatar
+ 3
It looks like it would probably run and perform the task you intended (bravo!) but it is horrible code in terms of effort, scalability and maintenance! Imagine the next step of development is to scale the code to 100 people! would you write out the code 100 times? What if new expenses need to be considered in the future... should you update the code 1 time for everyone or once each for every person's details? Testing would also be a nightmare ... you would need to check correct outputs for each of 100 people to make sure there were no typos or omissions in the code. Good code maximises reusability to minimise repetition. The secret is to use lists and loops (as already mentioned by @Hatsy Rei
5th Oct 2017, 1:31 AM
Ben-Davis
Ben-Davis - avatar
+ 2
thanks for info! I understand what you are telling me. looks like I need to study ip on how to use arrays
5th Oct 2017, 2:25 AM
Rick Wallace
Rick Wallace - avatar
+ 1
will this code be acceptable ? how can I improve it
4th Oct 2017, 10:25 PM
Rick Wallace
Rick Wallace - avatar
+ 1
arrays are useful but not always essential. I recommend you focus on using functions and loops for now. complex data structures can be learnt at the same time, or maybe later... modular reusable code is generally broken into functions (or maybe even into objects and instances but don't worry about that yet)
5th Oct 2017, 9:09 AM
Ben-Davis
Ben-Davis - avatar
+ 1
thinking in pseudo code can help (this is not actual code) for each *person* in the *team* do get_expense_data for get_expense_data do repeat while person not verified as employee * get person name * verify person as employee retrieve employee salary for expense in list of expenses do get expense name check name against approved expenses list get dollar value of expense check expense within range this is probably a bad example or implementation (and not complete). I recommend searching YouTube for how to use pseudo code and/or flow diagrams to plan/design software
5th Oct 2017, 9:30 AM
Ben-Davis
Ben-Davis - avatar
+ 1
thanks! so does using loop also mean to just call 1 variable for each expense such as gas, groceries...etc
5th Oct 2017, 7:05 PM
Rick Wallace
Rick Wallace - avatar