Python Forum
Cannot figure out what is wrong
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Cannot figure out what is wrong
#11
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'.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#12
(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.)
Reply
#13
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]
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Im so stressed because I cant figure out what I'm doing wrong. BursteXO 2 2,988 Jan-29-2018, 01:16 AM
Last Post: BursteXO

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020