Computer Science 110
Foundations of Computer Science

Denison

NextClass Assignment 2

This assignment follows in style to NextClass Assignment 1, demonstrating some of the next elements of learning a programming language. As before, you must type the given code, or you may be required to write your own simple set of steps to satisfy a problem description. Each set of steps should be placed in their own text file, named as requested and having a ".py" extension. If you get syntax errors, you must resolve them and you also must correct any logical errors, wherein your code may not "do the right thing."

Exercise 1

Enter the following Python program, naming it nc2-1.py. Explore the programming language concept of a "comment", online or using your textbook. Predict the output of the given program before you execute it.
# This program displays a person's
# name and address

print 'Kate Austen' # Name portion print '123 Dharma Lane' # Address portion print 'Asheville, NC 28899'

Since I am asking you to predict the output, I will refrain from showing the execution here. Experiment by changing the spacing of the comments and the print statements. Under what conditions might you get a syntax error? Does it make sense? Bring any behavior you find interesting to the class for us to discuss.

Exercise 2

In this next exercise, we start to see both extensive use of variables and some simple expressions in combination. Create nc2-2.py with the following Python program:
cars = 100
space_in_a_car = 4.0
drivers = 30
passengers = 90
cars_not_driven = cars - drivers
cars_driven = drivers
carpool_capacity = cars_driven * space_in_a_car
average_passengers_per_car = passengers / cars_driven

print "There are", cars, "cars available." print "There are only", drivers, "drivers available." print "There will be", cars_not_driven, "empty cars today." print "We can transport", carpool_capacity, "people today." print "We have", passengers, "to carpool today." print "We need to put about", average_passengers_per_car, "in each car."

The output should match the following:
[evaluate nc2-2.py]
There are 100 cars available.
There are only 30 drivers available.
There will be 70 empty cars today.
We can transport 120.0 people today.
We have 90 to carpool today.
We need to put about 3 in each car.

Explain to yourself why some of the output shows a decimal point and some of the output does not.

Exercise 3

The last exercise in this set demonstrates the use of the input() function to help program a simple game. It also uses some more advance features of the language that we have not studied yet, but that we can use to start to build our intuition of program operation. Enter the following progam in a file named nc2-3.py:

See if you can predict and/or reason about what the following program will do:


print "Welcome!"
guess = input("Guess the number: ")
if guess == 5:
    print "You win!"
else:
    print "Sorry, better luck next time."
print "Game over"

Try running your program at least 3 times, predicting the programs operation and printed result for each case. When prompted, enter 2, then 7, and then 5 during each run. Did you get the output you expected in all cases? Before you turn in this exercise, add comments with your name, the date, and description of the steps taken by the program.