0
Help how do i use "def" function?
i want to make a quiz with mathematics question and while iam browsing i found people are using a "def" function and i cant seem to know what is that
4 ответов
+ 3
def is not a function.
def is keyword you place before your function name to define a method or function.
Syntax
def <functionName>(parameter 1, parameter 2):
Code goes here
return anything
Eg. def add(a, b):
c = a+b
return c
add i s the function name
+ 1
ok thanks!
+ 1
You should check out the Python tutorial here if you don't know what "def" is. First of all, I believe it's a keyword used to define a function. A function is a block of code that is reusable. For example, as you are making a mathematics quiz, an example function would be this:
import random
def generate_question(): #function name will be generate_question, same rules with naming variables
operations = ["+", "-"]
operation = random.choice(operations) #chooses random operation from the list operations
return str(random.randint(100)) + operation + str(random.randint(100)) #gives function a value if you place it in a variable; returns a string containing two random numbers between 0 and 100 being added together, such as "19 + 65"
Now you can generate as many addition and subtraction problems as you want, by doing something like this:
one = generate_question()
0
https://www.sololearn.com/discuss/1363217/?ref=app
Yes, check out the Python tutorial, you can also see the above discussion