Computer Science 110
Foundations of Computer Science

Denison

NextClass Assignment 4

The objective of this assignment is to better understand the difference between the input() function and the raw_input() function, to learn about some built-in functions that can help with getting input from a user, and to practice with output using string formatting and controlling output.

My thanks to Zed Shaw for the inspiration for some of these exercises from his online and PDF books, "Learning Python the Hard Way".

If I ask any questions in what follows, use comments to put your answer within each of the python programs you submit.

Exercise 1

Enter the following Python program, naming it nc4-1.py.
myName = "TC Bressoud"
myAge = 35 # a lie
myHeight = 71
myWeight = 180
myEyes = 'Blue'
myHair = 'Brown'

print "Let's talk about %s" % myName
print "He's %d inches tall,\n and weighs %d pounds" % (myHeight, myWeight)
print "He's got %s eyes and %s hair." % (myEyes, myHair)
print "He's got", myEyes, "eyes and", myHair, "hair."

For each print statement, add a comment giving the number of expressions printed by the statement. ... be careful.

What did the \n cause to happen when the second print statement was executed?

Change the first '%d' to a '%f' and run the program again. What happens in the observed output? What do the '%d', '%f' and '%s' tell Python when constructing a formatted string?

Exercise 2

In the next exercise, we look at input() versus raw_input() in a program. Recall that, in class, we said that, in practice, we use input() when we want to retrieve a number from a user, and we use raw_input() when we want to retrieve a string from a user. One might wonder what happens if we don't follow that advice.

Enter the following Python program, naming it nc4-2.py.


x = raw_input('Enter a number x: ')
print 'The type of x is', type(x)

y = input('Enter a number y: ')
print 'The type of y is', type(y)

#a = x / 3.0
#b = y / 3.0

#print 'The type of a is ', type(a), "\nThe type of b is ", type(b)

Add a comment after the two print statements telling me the type reported. Note that we are also learning a new function, called type(), that takes a Python expression as an argument and reports (as a string that can be used in a print statement) Python's view of the data type of that expression.

Next, remove the comment characters in front to the three final statements in the program. Using comments, tell me what happened and reason about why we see the resultant behavior.

Next, add a line 'x = int(x)' immediately before the assignment to a, and add a print of the type of x somewhere after that line. Report and explain what you observe.

Exercise 3

Enter the following Python program, naming it nc4-3.py.


myValue = 3.14159265359
print '%.1f' % myValue
print '%.3f' % myValue

s = '%.6f' % myValue
print s, 'and the type of s is', type(s)
print myValue

When we use formatting, is the value associated with the formatted variable actually changed? If we limit the number of place in formatted output, how does Python determine the final digit?