Contents
- area.py
- input_demo.py
- range_analysis.py
- simple_for.py
- using_range.py
area.py 1/5
[top][prev][next]
# Finds the area of a rectangle
# by CS111 class
print("This program calculates the area of a rectangle.")
width = eval(input("Enter the width: "))
height = eval(input("Enter the height: "))
# To find the area of a rectangle, you multiply the rectangle's width by the height.
area = width * height
# display the results (the area)
#print("The rectangle's width is", width)
#print("The rectangle's height is", height)
print("The area of the rectangle is:", area)
input_demo.py 2/5
[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!")
range_analysis.py 3/5
[top][prev][next]
# Example of for loops using range
# by Sara Sprenkle
# Question: what does range do?
for i in range(10):
squared = i * i
print(i , "^2 =", squared)
print(i)
# QUESTION FOR CLASS:
# How is i changing each time through the loop?
# What happens if you change the variable from
# i to some other variable name?
simple_for.py 4/5
[top][prev][next]
# Examples of for loops using range
# by Sara Sprenkle
# The "chorus" gets repeated 3 times
for i in range(3):
print("You say 'hello'")
print("And, I say 'goodbye'...")
num_repetitions = 5
print()
# for loop with only one statement that gets repeated
for x in range(num_repetitions): print("Repeat the chorus!")
using_range.py 5/5
[top][prev][next]
# Examples of using range, with different numbers of parameters
# by Sara Sprenkle
#
print("------------ range(10) ------------")
for x in range(10):
print(x)
print("----------- range(5,10) -----------")
for y in range(5, 10):
print(y)
print("----------- range(1,10,1) -------------")
for x in range(1, 10, 1):
print(x)
# What happens if step is negative?
# What happens if stop < start?
Generated by GNU Enscript 1.6.6.