Contents
- ./average2.py
- ./average2_withmain.py
- ./practice1.py
- ./practice2.py
- ./practice3.py
./average2.py 1/5
[top][prev][next]
# Program to find the average of two numbers
# by Sara Sprenkle
def average2(num1, num2):
"""
Parameters: two numbers to be averaged
Returns the average of two numbers
"""
average = (num1 + num2)/2
return average
print("This program will find the average of two numbers.")
print()
num1 = eval(input("Enter the first number: " ))
num2 = eval(input("Enter the second number: "))
# calculate the average of the two numbers
average = average2(num1, num2)
print("The average of", num1, "and", num2, "is", average)
./average2_withmain.py 2/5
[top][prev][next]
# Program to find the average of two numbers.
# Demonstrates using a main function.
# by Sara Sprenkle
def main():
print("This program will find the average of two numbers.")
print()
num1 = eval(input("Enter the first number: " ))
num2 = eval(input("Enter the second number: "))
# calculate the average of the two numbers
average = average2(num1, num2)
print("The average of", num1, "and", num2, "is", average)
def average2(num1, num2):
"""
Parameters: two numbers to be averaged
Returns the average of two numbers
"""
average = (num1 + num2)/2
return average
main()
./practice1.py 3/5
[top][prev][next]
# Exercising your knowledge of variable scope.
#
def main():
num = eval(input("Enter a number to be squared: "))
squared = square(num)
print("The square is", squared)
def square(n):
return n * n
main()
./practice2.py 4/5
[top][prev][next]
# Exercising your knowledge of variable scope.
def main():
num = eval(input("Enter a number to be squared: "))
square(num)
print("The square is", computed)
def square(n):
computed = n*n
return computed
main()
./practice3.py 5/5
[top][prev][next]
# Exercising your knowledge of variable scope.
def main():
num = eval(input("Enter a number to be squared: "))
squared = square(num)
print("The square is", squared)
print("The original num was", n)
def square(n):
return n * n
main()
Generated by GNU Enscript 1.6.5.90.