From caeb8808beef32fd6230f5d4c7e3813f5a8a28d6 Mon Sep 17 00:00:00 2001 From: marcv Date: Tue, 25 Aug 2026 16:13:06 +0200 Subject: [PATCH] Add some examples to functions --- Defining_funtions.py | 28 +++++++++++++++++++++++++++- playground.py | 26 ++++++++++++++++++++++---- 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/Defining_funtions.py b/Defining_funtions.py index c78639b..354a63c 100644 --- a/Defining_funtions.py +++ b/Defining_funtions.py @@ -2,4 +2,30 @@ def hello(name): print(f'Hello {name}! How are you?') username = input('What is your name stranger?\n>') -hello(username) \ No newline at end of file +hello(username) + +# Another example using functions and return code: + +import random + +# Defining the function: +def get_answer(answer): + if answer <10: + return "The answer is smaller than 10" + elif answer == 10: + return "The answer is 10." + else: + return 'The answer is greater than 10' + +# Generating a random number, using that random number to define a variable by using it as an argument in the funtion, +# and printing the result: +random_number = random.randint(1, 20) +result = get_answer(random_number) +print(result) + +# The 3 lines above can be rewritten to a single line: +print(get_answer(random.randint(1, 20))) + +# And the check to see if the code is working as it should: +print(f"(It is {random_number})") + diff --git a/playground.py b/playground.py index bd7a708..1f01950 100644 --- a/playground.py +++ b/playground.py @@ -1,7 +1,25 @@ -def hello(name): - print(f'Hello {name}! How are you?') +# Again -username = input('What is your name stranger?\n>') -hello(username) +import random + +def get_answer(answer): + if answer <10: + return "The answer is smaller than 10" + elif answer == 10: + return "The answer is 10." + else: + return 'The answer is greater than 10' + +random_number = random.randint(1, 20) + +result = get_answer(random_number) +print(get_answer(random.randint(1, 20))) + +print(result) +print(f"(It is {random_number})") + + + + \ No newline at end of file