1def function():
2 print('This is a basic function')
3
4function()
5// Returns 'This is a basic function'
6
7def add(numA, numB):
8 print(numA+numB)
9
10add(1,2)
11// Returns 3
12
13def define(value):
14 return value
15
16example = define('Lorem ipsum')
17print(example)
18// Returns 'Lorem ipsum'
19
20
1# Python Functions
2def a_function(): #def FUNCTIONNAMEHERE(): Function content
3 print("Hello World!")
4 return 0
5def answerValid(prompt, correct_answers, invalid): # Functions can also have parameters, or variables that can be used only in tha function
6 while True:
7 user = input(prompt)
8 if user in correct_answers:
9 break
10 return user # Example Function
11 else:
12 print(invalid)
13
14a_function() # To call a function; Prints 'Hello World!'
15# But if this is done:
16function_called = a_function() # function_called would contain a 0, because the function returns 0
17answer = answerValid("What is 8 * 8?", [64], "Invalid Option")
18print(answer) # Prints 64, because that is the only possible answer
1def chess_piece(piece_name, piece_value, piece_rank):
2 """Info about a chess piece."""
3 print(f"\nThis chess piece is called the {piece_name}.")
4 print(f"It has an approximate value of {piece_value} points.")
5 print(f"In other words, this piece is the {piece_rank} valuable piece.")
6
7
8chess_piece('pawn', '1', 'least')
9chess_piece('knight', '3', 'second to last most')
10chess_piece('bishop', '3', 'third to last most')
11chess_piece('rook', '5', 'third most')
12chess_piece('queen', '9', 'second most')
13chess_piece('king', 'infinite', 'most')
14