Python_Chapter 04 Slides - Tagged
Python_Chapter 04 Slides - Tagged
Knowledge
5. In general terms, describe how to define a function, including the
use of a return statement.
6. In general terms, describe how to call a function.
7. In general terms, describe how to define and call a main()
function.
How to call it
print_welcome() # prints welcome message
How to call it
message = "Welcome to the Future Value Calculator"
print_welcome(message) # prints welcome message
How to call it
miles = 500
gallons = 14
mpg = calculate_miles_per_gallon(miles, gallons) #
35.7
def main():
# display a welcome message
print("The Future Value Calculator\n")
# get input
monthly_investment = float(input(
"Enter monthly investment: "))
yearly_interest = float(input(
"Enter yearly interest rate: "))
years = int(input(
"Enter number of years: "))
# display output
print("Future value: ", round(future_value, 2))
Continue? (y/n):
print("Future value:\t\t\t"
+ str(round(future_value, 2)))
print()
print("Bye!")
if __name__ == "__main__":
main()
return future_value
def main():
tax = calc_tax(85.0, .05) # tax is local variable
print("Tax:", tax) # Tax 4.25
def main():
calc_tax(85.0, .05)
print("Tax:", tax) # Tax 4.25 (global)
def main():
calc_tax(85.0, .05)
print("Tax:", tax) # Tax 0.0 (global)
def to_fahrenheit(celsius):
fahrenheit = celsius * 9/5 + 32
return fahrenheit
def to_fahrenheit(celsius):
"""
Accepts degrees Celsius (celsius argument)
Returns degrees Fahrenheit
"""
fahrenheit = celsius * 9/5 + 32
return fahrenheit
NAME
temperature
DESCRIPTION
This module contains functions for converting
temperature between degrees Fahrenheit
and degrees Celsius
FUNCTIONS
to_celsius(fahrenheit)
Accepts degrees Fahrenheit (fahrenheit argument)
Returns degrees Celsius
to_fahrenheit(celsius)
Accepts degrees Celsius (celsius argument)
Returns degrees Fahrenheit
def display_menu():
print("MENU")
print("1. Fahrenheit to Celsius")
print("2. Celsius to Fahrenhit")
print()
def convert_temp():
option = int(input("Enter a menu option: "))
if option == 1:
f = int(input("Enter degrees Fahrenheit: "))
c = temp.to_celsius(f)
c = round(c, 2)
print("Degrees Celsius:", c)
elif option == 2:
c = int(input("Enter degrees Celsius: "))
f = temp.to_fahrenheit(c)
f = round(f, 2)
print("Degrees Fahrenheit:", f)
else:
print("You must enter a valid menu number.")
if __name__ == "__main__":
main()
Your guess: 5
Too low.
Your guess: 8
You guessed it in 2 tries.
Bye!
LIMIT = 10
def display_title():
print("Guess the number!\n")
def play_game():
number = random.randint(1, LIMIT)
print("I'm thinking of a number from 1 to "
+ str(LIMIT) + "\n")
count = 1
while True:
guess = int(input("Your guess: "))
if guess < number:
print("Too low.")
count += 1
elif guess > number:
print("Too high.")
count += 1
elif guess == number:
print("You guessed it in "
+ str(count) + " tries.\n")
return
TURN 2
Roll or hold? (r/h): r
Die: 1
Turn over. No score.
...
...
You finished in 4 turns!
# global variables
turn = 1
score = 0
score_this_turn = 0
turn_over = False
game_over = False
def main():
display_rules()
play_game()
def display_rules():
print("Let's Play PIG!")
print()
print("* See how many turns it takes you to get to 20.")
print("* Turn ends when you hold or roll a 1.")
print("* If you roll a 1, you lose all points for the turn.")
print("* If you hold, you save all points for the turn.")
print()
def take_turn():
global turn_over
print("TURN", turn)
turn_over = False
while not turn_over:
choice = input("Roll or hold? (r/h): ")
if choice == "r":
roll_die()
elif choice == "h":
hold_turn()
else:
print("Invalid choice. Try again.")
def hold_turn():
global turn, score_this_turn, score, turn_over, game_over
print("Score for turn:", score_this_turn)
score += score_this_turn
score_this_turn = 0
print("Total score:", score, "\n")