Contents
- ./average2.py
- ./input_demo.py
- ./int_input.py
./average2.py 1/3
[top][prev][next]
# This program calculates and displays the average of two numbers
# by CS111 class
# 1st: hardcode the two numbers to average
# 2nd: change this to prompt the user to input two numbers
num1 = 1
num2 = 3
# add those two numbers together
sum = num1 + num2
# divide that sum by 2
avg = sum/2
# display the output
print("The average of", num1, "and", num2, "is", avg)
./input_demo.py 2/3
[top][prev][next]
# Demonstrate numeric and string input
# by Sara Sprenkle for CS111
color = input("What is your favorite color? ")
print("Cool! My favorite color is _light_", color, "!")
rating = float(input("On a scale of 1 to 10, how much do you like Zendaya? "))
print("Cool! I like her", rating*1.8, "much!")
print(rating)
./int_input.py 3/3
[top][prev][next]
# Requiring an integer as input.
# by Sara Sprenkle
str_age = input("Enter your age: ")
age = int(str_age)
# this will throw an exception if the str_age is not an int.
# We don't know how to handle exceptions yet.
Generated by GNU Enscript 1.6.5.90.