Contents

  1. arith_and_assign.py
  2. average2.py
  3. input_demo.py
  4. int_input.py

arith_and_assign.py 1/4

[
top][prev][next]
# Demonstrates arithmetic operations and assignment statements
# by Sara Sprenkle

x = 3
y = 5

print("x =", x)
print("y =", y)

result = x * y
print("x*y =", result)

# alternatively:
print("x*y =", x*y)


average2.py 2/4

[
top][prev][next]
# Finds the average of two numbers
# by CS111 class

print("This program computes the average of two numbers.")

# initialize the numbers
num1 = -2
num2 = 2

# display the values of the numbers
print("num1 =", num1)
print("num2 =", num2)

# add the two numbers together
total = num1 + num2

# divide the sum by two
average = total/2

print("The average of", num1, "and", num2, "is", average)

input_demo.py 3/4

[
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 = eval(input("On a scale of 1 to 10, how much do you like Chadwick Boseman? "))
print("Cool!  I like him", rating*1.8, "much!")


int_input.py 4/4

[
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.6.