Python Classroom
  • Introduction
  • About Python Classroom
  • Python Cloud Options
    • CS50 IDE
      • CS50 IDE Overview
      • CS50 IDE and Python
      • CS50 IDE Debugging
      • CS50 IDE Shortcuts
      • Linux Bash Shell
      • Vim Mode
        • Vim Tutorial
    • CS50 Sandbox
    • PythonAnywhere
    • Repl.it
  • Python Curriculum Map
    • Pedagogy
      • Python Professional Development
      • Teaching Tips
      • Assessment Tips
      • Rubrics
      • Activities
        • Picture Activity
        • Map Activity
        • Crossing the Bridge
        • NIM
        • Mastermind
        • Cards
          • Card Deck Algorithms
          • Sorting Cards
        • River Crossing
          • Jealous Boyfriends
          • Cannibals and Priests
          • Family
          • Police and the Thief
          • Humans and Monkeys
          • Moving Money
        • Crossing the River
        • Traveling Salesperson
        • Logic Problems
        • CIA Crack the Code
        • IQ Test
        • Puzzles
    • AP Computer Science Principles Framework
  • Python Philosphy
    • How to Practice Python
  • microbit
  • Turtle Graphics
    • Turtle Examples
    • Turtle Activities
    • Turtle Maze Problems
    • Turtle Graphics with loops
    • Turtle Snake
    • Turtle Graphics with Conditionals
  • Output
    • Output Examples
    • Output Mistakes
  • Variables
    • Variable Data Type Examples
    • Variable Role Examples
    • Variables Mistakes
    • Variables Problems
  • Math
    • Math Examples
    • Math Mistakes
    • Math Problems
    • Math Self Check
  • Input
    • Input Examples
    • Input Mistakes
    • Input Problems
  • Decisions
    • if
      • if Examples
      • if Mistakes
      • if Problems
    • if else
      • if else Examples
      • if else Problems
      • if / if else Problems
    • if elif else
      • if elif else Examples
      • if elif else Problems
    • nested if
      • nested if Examples
      • nested if Problems
    • Logical Operators
      • Logical Operators Examples
    • Adventure Game Project
  • Loops
    • while loop - count up
      • Examples
      • Problems
    • while loop - countdown
      • Examples
      • Problems
    • while loop - sentinel value
      • Problems
    • while loop - sentinel menu
      • Problems
    • for loop - range (one argument)
      • Examples
      • Problems
    • for loop - range (two arguments)
      • Problems
    • for loop - range (three arguments)
      • Problems
  • Lists
    • Lists - Numbers
      • Problems
    • Lists - Strings
      • Problems
      • Shopping Project
  • Dictionaries
  • String Methods
  • Functions
    • Variable Scope
    • Functions - no parameters
    • Functions - one parameter
    • Functions - return
    • Functions - lists
  • Files
  • Classes
    • Inheritance
  • Python Projects
    • Adventure Game
    • Restaurant Project
    • Trivia Game
    • Family Tree Project
  • Raspberry Pi
    • Raspberry Pi Models
    • Raspberry Pi Setup
  • Roblox
  • Glossary
Powered by GitBook
On this page
  • Example 1
  • Example 2
  • Example 3
  • Example 4

Was this helpful?

  1. Functions

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()
PreviousVariable ScopeNextFunctions - one parameter

Last updated 6 years ago

Was this helpful?