Python Forum
Please Help Understand Functions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Please Help Understand Functions
#4
first of all:
def enterNumbers():
    float(input("Type numbers: "))
    return
returns None, because you didn't tell it to return anything.
second:
float(input("Type numbers: "))
will fail, not written properly
so what you really want is (i changed function name to lower case, camel case should not be used for function names (PEP 8):
def enter_numbers():
    nums = input("Type numbers: ")
    nums = [float(item) for item in nums.split()]
    return nums
But now, nums will be a list of numbers (maybe only 1 element), but each will be a float
example:
Output:
enter_numbers() Type numbers: 1 2 3 4 print(numbers) [1.0, 2.0, 3.0, 4.0]
so code for enter_numbers needs to look like:
# Enter numbers and store them as "enterNumbers()" module?
def enterNumbers():
    nums = input("Type numbers: ")
    nums = [float(item) for item in nums.split()]
    return nums
And the other functions need to use this format:
# Enter numbers and store them as "enterNumbers()" module?
def enterNumbers():
    nums = input("Type numbers: ")
    nums = [float(item) for item in nums.split()]
    return nums


# This "calculate" module calls the "enterNumbers()" module, adds 1, and returns "a" which basically means "a" in the module IS "calculate()?"
def calculate():
    a = [item + 1.0 for item in enterNumbers()]
    return a


# This "main()" module calls both module to calculate?
def main():
    numbers = calculate()
    print(numbers)


if __name__ in '__main__':
    main()
if run:
Output:
Type numbers: 1 2 3 4 5 [2.0, 3.0, 4.0, 5.0, 6.0]
Reply


Messages In This Thread
Please Help Understand Functions - by toxicxarrow - Feb-21-2018, 08:23 PM
RE: Please Help Understand Functions - by Larz60+ - Feb-21-2018, 08:47 PM
RE: Please Help Understand Functions - by Larz60+ - Feb-21-2018, 10:06 PM
RE: Please Help Understand Functions - by nilamo - Feb-21-2018, 10:26 PM

Forum Jump:

User Panel Messages

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