5 Replies - 601 Views - Last Post: 04 October 2017 - 04:08 PM

#1 redsfan  Icon User is offline

  • New D.I.C Head

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

Newbie!

Posted 04 October 2017 - 10:37 AM

I am 50 years old and am trying to learn python programming. I was encouraged and told that it was the easiest programming language and "You can do it".....I am really struggling and just can not grasp the usage of functions and how to structure simple codes. I am taking a beginner course, find myself rereading chapters, watching youtube tutorials.....I came here hopefully to get some help, thanks!
Is This A Good Question/Topic? 0
  • +

Replies To: Newbie!

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13489
  • View blog
  • Posts: 53,886
  • Joined: 12-June 08

Re: Newbie!

Posted 04 October 2017 - 10:40 AM

Welcome!

At the core - functions breakup repetitive chunks of code. This means you can do some specific action and call it where ever in your program without having to copy/paste it over and over again.. this helps with debugging, maintenance, etc.
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!

Posted 04 October 2017 - 10:49 AM

# 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.")

:code:

This post has been edited by modi123_1: 04 October 2017 - 10:55 AM

Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is online

  • Chinga la migra
  • member icon


Reputation: 10686
  • View blog
  • Posts: 18,302
  • Joined: 19-March 11

Re: Newbie!

Posted 04 October 2017 - 01:51 PM

View Postmodi123_1, on 04 October 2017 - 12:40 PM, said:

Welcome!

At the core - functions breakup repetitive chunks of code. This means you can do some specific action and call it where ever in your program without having to copy/paste it over and over again.. this helps with debugging, maintenance, etc.


Added benefits of functions:
  • functions allow you to name a behavior. This allows you and anyone else looking at the code to understand not just how you're doing a thing, but what you meant to do.
  • functions allow you to encapsulate a behavior: if you define a function that uses only its input parameters and its local variables, you can understand its behavior in terms of those input parameters, because its local variables don't change. This makes debugging easier, since the things you have to think about are clearly defined, and you don't have to think about the state of the whole program all at once.
  • functions, by allowing you to name a behavior, allow you to think about your program at a higher level. Instead of thinking about a sequence of python instructions, you're thinking about actions that you've named. This is the difference between "get the coffee beans off the shelf, put the coffee beans in the grinder, grind the coffee, put the ground coffee in the cone filter, put the cone filter on the coffee receptacle, boil the water, pour the water over the coffee grounds, pour the coffee into a cup, add cream, drink coffee" and enjoying a nice cup of joe.

Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky  Icon User is online

  • Chinga la migra
  • member icon


Reputation: 10686
  • View blog
  • Posts: 18,302
  • Joined: 19-March 11

Re: Newbie!

Posted 04 October 2017 - 01:59 PM

Looking at the code you've posted, there's some room for discussion if you want suggestions for improvement, but I'd ask you to open a topic under Python for that conversation. Please be sure to ask a question when you do, so people will know what you want help with.
Was This Post Helpful? 0
  • +
  • -

#6 redsfan  Icon User is offline

  • New D.I.C Head

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

Re: Newbie!

Posted 04 October 2017 - 04:08 PM

Thank you! I know this is very basic and I am beginning code.I know there is some room for improvement here
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1