14 Replies - 986 Views - Last Post: 10 October 2017 - 12:20 PM Rate Topic: -----

#1 redsfan  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 04-October 17

Newbie needed some help cleaning up an exercise

Posted 04 October 2017 - 04:17 PM

# beginner working on this program, any help cleaning up or advice is greatly apreciated

# weekly budget for 3 people
This is a huge mess, I know!
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 money to save:" savings3)
if savings1 >= left_over1 :
              print("Congratulations, you are on your way to financial freedom!")
              else:
              print("Name1, need to save more money.")
  if savings2 >= left_over2 :            
              print("Congratulations, you are on your way to financial freedom!")
              else:
              print("Name2, need to save more money.")
    if  savings2 >= left_over2 :
              print("Congratulations, you are on your way to financial freedom!")
              else:
              print("Name3, need to save more money.")




Is This A Good Question/Topic? 0
  • +

Replies To: Newbie needed some help cleaning up an exercise

#2 jon.kiparsky  Icon User is online

  • Chinga la migra
  • member icon


Reputation: 10682
  • View blog
  • Posts: 18,296
  • Joined: 19-March 11

Re: Newbie needed some help cleaning up an exercise

Posted 04 October 2017 - 06:28 PM

Say more. What is it that you're asking for help with? What makes you think this isn't just perfect as it is?

Also, please try to specify what your program is meant to do. It's possible to make some guesses based on reading the code, but it's important that you get in the habit of communicating when you're looking for help. It'll save you a lot of time.
Was This Post Helpful? 0
  • +
  • -

#3 redsfan  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 04-October 17

Re: Newbie needed some help cleaning up an exercise

Posted 04 October 2017 - 07:14 PM

I honest feel like it was over done and could be simpmofied with maybe arrays, but not really sure yet how I would do that
Was This Post Helpful? 0
  • +
  • -

#4 DblAAssassin  Icon User is offline

  • D.I.C Regular

Reputation: 43
  • View blog
  • Posts: 322
  • Joined: 11-May 13

Re: Newbie needed some help cleaning up an exercise

Posted 04 October 2017 - 07:19 PM

I would suggest adding white space and comments to make it more readable.

For example
#Asks the user for 3 names, each name is store in it's own variable.
name1 = input("what is your name? ")
name2 = input("what is your name? ")
name3 = input("What is your name? ")

#Asks the user to enter in the salaries of the three peopled name above, each salary is stored in it's own salary.
salary1 = eval(input("What is name1 weekly salary? ")
salary2 = eval(input("What is name2 weekly salary? ")
salary3 = eval(input("What is name3 weekly salary? ")



To improve the code you could make it look like this.
>> What is your name? Conner
...
>> What is Conner's weekly salary?


This post has been edited by DblAAssassin: 04 October 2017 - 07:21 PM

Was This Post Helpful? 1
  • +
  • -

#5 jon.kiparsky  Icon User is online

  • Chinga la migra
  • member icon


Reputation: 10682
  • View blog
  • Posts: 18,296
  • Joined: 19-March 11

Re: Newbie needed some help cleaning up an exercise

Posted 04 October 2017 - 07:45 PM

Okay, it's important that you try to be more specific, and that you focus your requirements on things the user cares about, but let's get started and see what we can get to.

The first thing I would suggest is that you remove about 2/3 of your program. Specifically, remove every line that contains a numeral that isn't 1, and then delete all of the 1s.
This will simplify things immensely. Once you've done that, you can indent the whole mess, put a def line at the top, and you have a function which does something. What does it do? Well, I don't have a great name for it, because it does a lot of stuff, but I can see that it gets input and it calculates stuff and it sends some strings to the output, so let's call it get_input_and_calculate_financial_projections_and_display_them
That'll do for now anyway.

Once you've done those things, you'll be able to roll through that loop as many times as you want, and it'll do this sort of projection for as many users as you want. You've got three baked into your original, so let's stick with that. Now your main loop looks like this:

for _ in range(3):
    get_input_and_calculate_financial_projections_and_display_them()



And it does ALMOST exactly what you had before, but I think you'll find it's at least 50% neater than it was before, once you've made these changes. Why don't you go ahead and try that and then post the result and we'll see where you want to go next.
Was This Post Helpful? 1
  • +
  • -

#6 redsfan  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 04-October 17

Re: Newbie needed some help cleaning up an exercise

Posted 04 October 2017 - 07:55 PM

Thank you!I will take your input and work on it
Was This Post Helpful? 1
  • +
  • -

#7 andrewsw  Icon User is offline

  • the case is sol-ved
  • member icon

Reputation: 6376
  • View blog
  • Posts: 25,765
  • Joined: 12-December 12

Re: Newbie needed some help cleaning up an exercise

Posted 04 October 2017 - 11:38 PM

(Have you attempted to run the code? Mismatched parentheses and incorrect indenting will raise lots of errors.)
Was This Post Helpful? 0
  • +
  • -

#8 jon.kiparsky  Icon User is online

  • Chinga la migra
  • member icon


Reputation: 10682
  • View blog
  • Posts: 18,296
  • Joined: 19-March 11

Re: Newbie needed some help cleaning up an exercise

Posted 05 October 2017 - 05:32 AM

View PostDblAAssassin, on 04 October 2017 - 09:19 PM, said:

I would suggest adding white space and comments to make it more readable.


Comments are fine as an intermediate step, but my preference is to write clear code. To me, a comment is usually a challenge: how can I rewrite this code to make it perfectly obvious what we're doing here and why, so the comment becomes superfluous?
Was This Post Helpful? 2
  • +
  • -

#9 redsfan  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 04-October 17

Re: Newbie needed some help cleaning up an exercise

Posted 09 October 2017 - 11:18 AM

Ok, so I have spent many hours and have cleaned this up alot, However, I have reached a point where everything I try fails and I am chasing my tail. As you can see i get a salary not defined error and the final calculations give me zeros for each field? Any advice on this would be greatly appreciated


 # Get the names and salary for 3 people
>>> # create 4 expenses for each person and have them equal a variable that is  named weekly_expenses
>>> # subtract the weekly_expenses from salary to get left over money
>>> # have each person save 10% from salary
>>> # create a loop or condition to compare savings to left over money
>>> # If amount left over is less than amount saved say "You need to save more"
>>> # If amount left over is greater than amount saved say "congrats on savings"
>>> def get_input_and_calculate_financial_projections_and_display_them ():
	name = 0
	input("What is your name? ")
	salary = 0
	input("What is your weekly salary? ")
	gas = 0
	electric = 0
	food = 0
	phone = 0
	eval(input("Enter the four expenses gas,electric,food,phone seperated by commas: "))
	weekly_expenses = gas + electric + food + phone
	left_over = salary - weekly_expenses
	savings = salary * .10
	print("Name:", name, "weekly salary:", salary, "expenses:", weekly_expenses, "salary after expenses:", left_over, "amount of money to save:", savings)

	
>>> if savings >= left_over:
	print("Congratulations, you are on your way to financial freedom!")
else:
	print(name, "You need to save more money.")

	
Traceback (most recent call last):
  File "<pyshell#24>", line 1, in <module>
    if savings >= left_over:
NameError: name 'savings' is not defined
>>> for _ in range(3):
	get_input_and_calculate_financial_projections_and_display_them()
:code:


What is your name? Tom
What is your weekly salary? 700
Enter the four expenses gas,electric,food,phone seperated by commas: 40,30,100,25
Name: 0 weekly salary: 0 expenses: 0 salary after expenses: 0 amount of money to save: 0.0
What is your name?

This post has been edited by modi123_1: 09 October 2017 - 11:19 AM
Reason for edit:: In the future, please use the [code] tag button in the editor.

Was This Post Helpful? 0
  • +
  • -

#10 andrewsw  Icon User is offline

  • the case is sol-ved
  • member icon

Reputation: 6376
  • View blog
  • Posts: 25,765
  • Joined: 12-December 12

Re: Newbie needed some help cleaning up an exercise

Posted 09 October 2017 - 11:48 AM

On lines 10 and 12 you are asking for input but not doing anything with the values received, such as storing them in variables. The values entered are just discarded.

Similarly for line 17 but, further, you ask for four values but have no code that would parse the entered text into four distinct values/ variables.

Take a step back. Ask for a simple number, store it, double it, and print out the doubled value. Then you could return to your assignment.
Was This Post Helpful? 1
  • +
  • -

#11 redsfan  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 04-October 17

Re: Newbie needed some help cleaning up an exercise

Posted 10 October 2017 - 11:38 AM

I am still struggling with exactly how to run the loop and store the input into variables. Use get name for example, how do you take the input name and store in variable to use later???
Was This Post Helpful? 0
  • +
  • -

#12 ndc85430  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 588
  • View blog
  • Posts: 2,479
  • Joined: 13-June 14

Re: Newbie needed some help cleaning up an exercise

Posted 10 October 2017 - 11:48 AM

Assign the return value of input to a variable.
Was This Post Helpful? 0
  • +
  • -

#13 redsfan  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 04-October 17

Re: Newbie needed some help cleaning up an exercise

Posted 10 October 2017 - 11:52 AM

Can I please get an example...
Was This Post Helpful? 0
  • +
  • -

#14 jon.kiparsky  Icon User is online

  • Chinga la migra
  • member icon


Reputation: 10682
  • View blog
  • Posts: 18,296
  • Joined: 19-March 11

Re: Newbie needed some help cleaning up an exercise

Posted 10 October 2017 - 11:56 AM

This line:

input("What is your name? ")



prompts the user for input, and returns the input

Since you've done nothing with the input, it's discarded.

This line:

name = input("What is your name? ")



prompts the user for input and assigns the returned value to the variable called name
Was This Post Helpful? 1
  • +
  • -

#15 redsfan  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 04-October 17

Re: Newbie needed some help cleaning up an exercise

Posted 10 October 2017 - 12:20 PM

Thank you Jon. So by making this change, will it store a new value for name each time it iterates, or like in my case, with range set at three, will it store all three variables??also, love the screw trump message, totally concur!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1