Ask coding questions

← Back to all posts
Modules/Functions in Python
Gooftroop21 (0)

https://repl.it/@Gooftroop21/algo2py?language=python3&folderId=

I'm doing some exercises for homework with one of my questions asks me to write a statement that calls the module, passing 12 as an argument.

Commentshotnewtop
GavHern (65)

There is no module in this code. To add a module, write import and then the name of the module. For example:

import random

now, I could use the random functions given in that module, like `random.randint`. You may pass parameters into them regularly as shown:

import random

randomnum = random.randint(1,10)

print(randomnum)

This prints a random number from 1-10. You can, of course, import any module and call any function from it. It goes modulename.functionname(parameters).

Hope this helped!

Gooftroop21 (0)

@GavHern So I have to create a module on the side with the "Main.py" code is? and so I write the "quantity = 12" in the main so that it is global or in the module so that it is local?

GavHern (65)

@Gooftroop21 oh, I think I misinterperated what you meant. quantity is local and cant be referenced in other files. The way you reference code in other files is through importing them with import main. (main is an example, if you had bananas.py you would say import bananas).

Gooftroop21 (0)

@GavHern I'm sorry, I'm really new to Python and my course is going super fast and we don't really practice/learn python class but I need to get my homework finish.. xD I'll try to understand better.. But thank you. Didn't mean to waste your time.

GavHern (65)

@Gooftroop21 You're all good. I wouldn't be here if I didn't want to help people out. Best of luck on your course, always come back to repl talk if you ever need more help.

Gooftroop21 (0)

@GavHern Thank you, same to you.