Python Forum
Please Help Understand Functions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Please Help Understand Functions
#1
I had an assignment that required me to perform a task using only modules. I turned in a botched version, but I would actually like to learn how to do it, since it seems important.

Goal: Get numbers from user, add one, display result; all while using modules.

# Enter numbers and store them as "enterNumbers()" module?
def enterNumbers():
    float(input("Type numbers: "))
    return

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

# This "main()" module calls both module to calculate?
def main():
    enterNumbers()
    calculate()
    return

# This runs all of the modules?
main()
My code seems to duplicate "enter numbers" twice. I'm pretty sure it's because I have "enterNumbers" in the "calculate()" module, as well as the "main()" module. To me, it seems like "a" is defining "enterNumbers() + 1", so I'm not trying to call it, only use it's stored data. Also, i feel like I'm missing something in the () of one of the modules to make it work right, I just can't figure out what.

If anyone would be kind enough to walk me through my code, keeping it as close as possible to the original while using modules, that would be appreciated.
Reply
#2
you're not duplicating enterNumbers
it is(def)inded once, you then call it from calculate
You call it from mian, but shouldn't,
remove that statement
Reply
#3
Ok, got rid of the duplication. Still getting this error:

C:\Python\python.exe C:/Users/-----/Desktop/Test.py
Type numbers: 5
Error:
Traceback (most recent call last): File "C:/Users/Vadim/Desktop/Test.py", line 13, in <module> main() File "C:/Users/Vadim/Desktop/Test.py", line 10, in main calculate() File "C:/Users/Vadim/Desktop/Test.py", line 6, in calculate a = enterNumbers() + 1 TypeError: unsupported operand type(s) for +: 'NoneType' and 'int' Process finished with exit code 1
Reply
#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
#5
(Feb-21-2018, 08:23 PM)toxicxarrow Wrote: keeping it as close as possible to the original while using modules, that would be appreciated.

What are you or your class defining as a "module"? As is, you don't have any, so there's not any to fix.
You have a few functions, but no modules. Those are not the same thing.
Reply


Forum Jump:

User Panel Messages

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