Add some examples to functions
This commit is contained in:
@@ -2,4 +2,30 @@ def hello(name):
|
||||
print(f'Hello {name}! How are you?')
|
||||
|
||||
username = input('What is your name stranger?\n>')
|
||||
hello(username)
|
||||
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})")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user