factorial.py
Output:
#defines a function that calculates the factorial
def factorial(n):
if n <= 1:
return 1
return n*factorial(n-1)
print "2! = ",factorial(2)
print "3! = ",factorial(3)
print "4! = ",factorial(4)
print "5! = ",factorial(5)
temperature2.py
2! = 2
3! = 6
4! = 24
5! = 120
Esecuzione:
#converts temperature to fahrenheit or celsius
def print_options():
print "Options:"
print " 'p' print options"
print " 'c' convert from celsius"
print " 'f' convert from fahrenheit"
print " 'q' quit the program"
def celsius_to_fahrenheit(c_temp):
return 9.0/5.0*c_temp+32
def fahrenheit_to_celsius(f_temp):
return (f_temp - 32.0)*5.0/9.0
choice = "p"
while choice != "q":
if choice == "c":
temp = input("Celsius temperature:")
print "Fahrenheit:",celsius_to_fahrenheit(temp)
elif choice == "f":
temp = input("Fahrenheit temperature:")
print "Celsius:",fahrenheit_to_celsius(temp)
elif choice != "q":
print_options()
choice = raw_input("option:")
area2.py
> python temperature2.py
Options:
'p' print options
'c' convert from celsius
'f' convert from fahrenheit
'q' quit the program
option:c
Celsius temperature:30
Fahrenheit: 86.0
option:f
Fahrenheit temperature:60
Celsius: 15.5555555556
option:q
Esecuzione:
#By Amos Satterlee
print
def hello():
print 'Hello!'
def area(width,height):
return width*height
def print_welcome(name):
print 'Welcome,',name
name = raw_input('Your Name: ')
hello(),
print_welcome(name)
print
print 'To find the area of a rectangle,'
print 'Enter the width and height below.'
print
w = input('Width: ')
while w <= 0:
print 'Must be a positive number'
w = input('Width: ')
h = input('Height: ')
while h <= 0:
print 'Must be a positive number'
h = input('Height: ')
print 'Width =',w,' Height =',h,' so Area =',area(w,h)
Your Name: Josh
Hello!
Welcome, Josh
To find the area of a rectangle,
Enter the width and height below.
Width: -4
Must be a positive number
Width: 4
Height: 3
Width = 4 Height = 3 so Area = 12