return is a value. in order to use it you need to assign it to a variable:
def ask_weight():
lbs = float(input("How many pounds do you weigh? "))
return lbs
weight = ask_weight()
print str(weight)
If you are using Python 2.x as was suggested, you should be using 'raw_input' rather than 'input'.
(Dec-12-2017, 01:27 AM)sparkz_alot Wrote: [ -> ]return is a value. in order to use it you need to assign it to a variable:
def ask_weight():
lbs = float(input("How many pounds do you weigh? "))
return lbs
weight = ask_weight()
print str(weight)
If you are using Python 2.x as was suggested, you should be using 'raw_input' rather than 'input'.
Alright, you are going to hate me for this... First of all, I do have an idea of return functions, I just am not able to realize a difference between them and prints. Secondly, I found out the reason why nothing popped up was because of line 39 and this wonderful command called break. As it was defining my functions, I think it read break as a cue to stop defining and to end the program. The second I removed the break command, it started working.
Thank you for teaching me so much though (especially the format command, that will be a life saver in the future.)
Consider this:
def pow(number, power): # calculating an exponent
result = 1
for _ in range(power):
result = result * number
# the function always returns a value. If you do not specify what will be that value it will be None.
# So, you may print it or just generate it some way. But outside of the function, this variable will be untouchable.
# If you put raw materials in some kind of super duper sophisticated machine it may produce a laptop.
# But if there is no functionality to get the production out good for you - no laptop. :D
# So here it is - the return statement
return result
powers = []
for num in range(1,5):
power = pow(num, 3) # here the result of the pow() is returned and assgned to a variable.
powers.append(power)
print(powers)
Output:
[1, 8, 27, 64]