We've already used plenty of functions so far, but it's time for you to really understand how to create your own functions and see how you can work them (e.g. call them from various places). Functions are typically created using the def command. You will need to memorise the way they are created so you can create your own.
Download Python 3 here:
1.First, run the code to see how it works!. We have created three functions and each one calls the next function. e.g. The part1 function calls the part2 function.
2. Add two more functions, part3 and part4 (make sure you don't have spaces in the function names. For instance, "part 4" will cause an error, so it must be "part4".
3. In each function part3 and part4, call the next function. The part3 function can call part 4, and part4 function can say "I am the last function. Goodbye".
4. Consider what would happen if you put a function call to the mainmenu() in the function part4. What do you think will happen?
def mainmenu(): print("This is the Main Menu") part1() #This is calling function part1 def part1(): print("This is a function called part 1") part2() def part2(): print("This is a function called part 2") mainmenu() #This is the starting point for the program. It tells Python to start at function mainmenu
1.Run the code below and note that it starts with the bank function, and then takes the user to their account. It then asks for a password, but then calls the denied() function. This doesn't exist yet, so it comes up with an error
2. Add the denied function (def denied():) and inside it, have a message which prints: "Sorry, access denied. Goodbye"
3. Consider what would happen if you changed the function call right at the bottom to account() instead of bank(). What will occur?
import time def bank(): print("Welcome to your bank!") print("Verifying account number:>>") time.sleep(1.4) print(".................") print("Verifying password:>>") time.sleep(1.4) print(".....Logging you in to your account") time.sleep(1.4) print("===Please Wait===") time.sleep(1.4) print(".................") account() def account(): print("========WELCOME TO YOUR ACCOUNT======") password=input("Enter secure password:") denied() bank()
1.There are three mistakes in the code below, can you spot them, fix them and get the program to run.
2. The first error is on line 1 where it says def main() ....what is missing?
3. The second error is line 5, where it says print("This is function1"). What is wrong with this line? It is inside a function, so where should it be?
4. The third eror is in line 8 where it says def function2: ....what is missing here from this function definition?
def main() print("This is the main function" def function1(): print("This is function1") def function2: print("This is function2") main()
1.This function will fail to run. You need to add a function call to welcome() somewhere. Can you add this to the code, to make the program run?
2. Create another function called goodbye. Inside it, print "Goodbye, nice to meet you". Call this function from the welcome function, so it jumps straight to it, once the welcome message is displayed.
def welcome(): print("Welcome")
1.Run the code below and see how it works. It takes you to a function called topthree, which lists the top three phones (add these yourself), and then is meant to take you to another function that lists the worst phone. It stops here, because the 'worst' function has not yet been created.
2.Create a function called worst. (e.g. def worst(): ) ....Inside it print the name of the worst phone (in your opinion of course!).
import time def main(): print("---LATEST PHONES.COM----") time.sleep(1.3) print("printing list of top phones...please wait.....") topthree() def topthree(): print("Top Three phones are:") #add the top three, in your opinion, here time.sleep(1.3) print("...now we are going to list the worst phone ever...please wait") worst() main()
Use your knowledge and skills from the completed tasks. The empty trinket below can be used to complete the following challenge. Show your teacher when complete!
1. Create a Main Menu with three options 1. Login 2. Register. 3. Quit
2. Create functions for login, register and quit. In each of them, include a simple message that describes what the function does. Get the main function to jump straight to the register function, and the register function to jump to the login function and finally place a function call in the login function to the quit function. The quit function can simply contain a message that says "Goodbye". You could also use import sys (at the very start of the program) and use the command sys.exit() in the quit function to actually exit the program.