Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
February 20th, 2013, 01:20 PM
 Th3C00lk1d
Registered User

Join Date: Feb 2013
Posts: 2
Time spent in forums: 19 m 29 sec
Reputation Power: 0
What the hell is this?!?!

Why the freakin hell won't this program work?

Code:
```# This program will prompt the user to enter 2 scores and calculate the average.

def getScores():
score1 = float(input("Enter the first score: "))
score2 = float(input("Enter the second score: "))
average = calcAverage(score1, score2)
print ("The average is: ", average)

def calcAverage(score1, score2):
average = (score1+score2)/2

def main():
getScores()
main()
```

#2
February 20th, 2013, 01:32 PM
 dariyoosh
Contributing User

Join Date: Nov 2012
Location: Iran
Posts: 148
Time spent in forums: 2 Days 15 h 7 m 17 sec
Reputation Power: 139
Because your average function doesn't return the average value (no return statement explicitly written in the function = return None), it should be something like this:

Code:
```def calcAverage(score1, score2):
return (score1+score2)/2```

The title of the topics created on this forum are used for those who do search before creating a new topic, so obviously, the title must somehow reflect what the topic is about.

Reading your topic title " What the hell is this?!?!" doesn't seem to conform to this best practice rule.

Regards,
Dariyoosh

Last edited by dariyoosh : February 20th, 2013 at 01:41 PM.

#3
February 20th, 2013, 01:50 PM
 Th3C00lk1d
Registered User

Join Date: Feb 2013
Posts: 2
Time spent in forums: 19 m 29 sec
Reputation Power: 0
Thanks for the help. However, if I may ask, what exactly is a return statement and when would I use a return statement?

#4
February 20th, 2013, 02:24 PM
 PythonNewb
Registered User

Join Date: Feb 2013
Posts: 15
Time spent in forums: 1 h 54 m 19 sec
Reputation Power: 0
I am pretty sure the return statement calls on the function, while a print will produce the text. Somebody correct me if I am wrong...

#5
February 20th, 2013, 03:40 PM
 dariyoosh
Contributing User

Join Date: Nov 2012
Location: Iran
Posts: 148
Time spent in forums: 2 Days 15 h 7 m 17 sec
Reputation Power: 139
Quote:
 Originally Posted by Th3C00lk1d Thanks for the help. However, if I may ask, what exactly is a return statement and when would I use a return statement?

According to Python (3) online tutorial
http://docs.python.org/3.0/tutorial...ining-functions
Quote:
 ... The return statement returns with a value from a function. return without an expression argument returns None. Falling off the end of a function also returns None ...

For example, let's say you define a function that takes as parameters two integers and calculates their product.

The mathematical syntax would be something like this:

productFun(x,y) = x * y

In python you write

Code:
```def productFun(x, y):
return x * y```

So thanks to the return statement, I may later write elsewhere in my code, for example:

Code:
```. . .
# so var will receive the value of 12 * 10 returned by the return statement of the function
var = productFun(12,  10) ```

Regards,
Dariyoosh

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > What the hell is this?!?!