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)

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

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

average2.py 2/4

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

# hardcode numbers
first = 2
second = 2

# add the two numbers together 
total = first + second
# and divide by 2
average = total / 2

# display the result
print("Average of", first, "and", second, "=", 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 Ryan Gosling? "))
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.