Functions - no parameters

intro.py
def main():
  done = False
  while not done:
    print("Menu")
    print("D - Do Now")
    print("Q - Quit")
    choice = input("Choice: ")
    if choice == "D":
        print("Do Now")
        name = input("What's your name")
        print(name,"welcome back!")
    elif choice == "Q":
        print("Exiting Game!")
        done = True

main()

Example 1

Create a function named e1.

Using a for loop, print your name 5 times.

Call the function e1 within the main function.

example1.py
def main():
  done = False
  while not done:
    print("Menu")
    print("D - Do Now")
    print("E1 - Example 1")
    print("Q - Quit")
    choice = input("Choice: ")
    if choice == "D":
        print("Do Now")
        name = input("What's your name")
        print(name,"welcome back!")
    elif choice == "Q":
        print("Exiting Game!")
        done = True
    elif choice == "E1":
        e1()

def e1():
  name = input("What's your name: ")
  for i in range(5):
    print(name)

main()

Example 2

Create a function named e2.

Using a for loop, print numbers from 0 to 4.

Call the function e2 within the main function.

example2.py
def main():
  done = False
  while not done:
    print("Menu")
    print("D - Do Now")
    print("E2 - Example 2")
    print("Q - Quit")
    choice = input("Choice: ")
    if choice == "D":
        print("Do Now")
        name = input("What's your name")
        print(name,"welcome back!")
    elif choice == "Q":
        print("Exiting Game!")
        done = True
    elif choice == "E2":
        e2()

def e2():
  for i in range(5):
    print(i)

main()

Example 3

Create a function named e3.

Using a for loop, create a table to show the numbers -5 to 5 and their squares. Create a header

print("Number\tSquared")

Call the function e3 within the main function.

example3.py
def main():
  done = False
  while not done:
    print("Menu")
    print("D - Do Now")
    print("E3 - Example 3")
    print("Q - Quit")
    choice = input("Choice: ")
    if choice == "D":
        print("Do Now")
        name = input("What's your name")
        print(name,"welcome back!")
    elif choice == "Q":
        print("Exiting Game!")
        done = True
    elif choice == "E3":
        e3()

def e3():
  print("Number\tSquared")
  for i in range(-5, 6):
    print(i,"\t",i**2)

main()

Example 4

Create a function named e4.

Write Python code that converts Celsius to Fahrenheit for the following values of Celsius: 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100.

Use the following formula:

F=9/5C+32

Create a header:

print("Celsius\tFahrenheit")

Call the function e4 within the main function.

example4.py
def main():
  done = False
  while not done:
    print("Menu")
    print("D - Do Now")
    print("E4 - Example 4")
    print("Q - Quit")
    choice = input("Choice: ")
    if choice == "D":
        print("Do Now")
        name = input("What's your name")
        print(name,"welcome back!")
    elif choice == "Q":
        print("Exiting Game!")
        done = True
    elif choice == "E4":
        e4()

def e4():
  print("Celsius\tFahrenheit")
  for x in range(0,101,10):
    print(x,"\t",9/5*x + 32)

main()

Last updated