Back to the Useless Trivia Program


You now know everything you need to know to program the project Useless Trivia from the beginning of the chapter. I'll present the program a little differently than the others. Instead of listing the code out in its entirety, I'll go over the program one section at a time.

Creating the Initial Comments

Although comments don't have any effect while the program runs, they are an important part of every project. As always, I list the program's purpose, my name, and the date I wrote the code:

 # Useless Trivia # # Gets personal information from the user and then # prints true, but useless facts about him or her # # Michael Dawson - 12/4/02 

TRICK

Experienced programmers also use the initial comments area to describe any modifications they make to code over time. This provides a great history of the program right up front. This practice is especially helpful when several programmers have their hands on the same code.

Getting the User Input

Using the raw_input() function, the program gets the user's name, age, and weight:

 name = raw_input("Hi. What's your name? ") age = raw_input("And how old are you? ") age = int(age) weight = raw_input("Okay, last question. How many pounds do you weigh? ") weight = int(weight) 

Remember, raw_input() always returns a string. Since age and weight will be treated as numbers, they must be converted. I broke up this process into two lines for each variable. First, I assigned the string from raw_input() to a variable. Then, I converted that string to an integer and assigned it to the variable again. I could have done both the assignments in one line, but I felt it's clearer this way.

Printing Lowercase and Uppercase Versions of name

The following lines print a version of name in uppercase and a version in lowercase with the help of string methods:

 print "\nIf poet ee cummings were to email you, he'd address you as", name.lower() ee_mad = name.upper() print "But if ee were mad, he'd call you", ee_mad 

In the uppercase version, I assigned the value to the variable ee_mad before printing. As you can see from the lowercase version before it, it's not necessary to use a variable. But I think it makes it clearer.

ee cummings, by the way, was an experimental American poet who didn't use uppercase letters. So, if he were alive and e-mailing you, he'd probably use all lowercase letters in your name. But if he were mad, he'd probably make an exception and "shout" via e-mail by addressing you in uppercase.

Calculating dog_years

The user's age in dog years is calculated and printed out:

 dog_years = age / 7 print _\nDid you know that you're just", dog_years, "in dog years?" 

It's a common belief that seven human years is equal to one dog year. So, in the first line, I divide age by 7 and assign that value to dog_years. Since 7 and age are both integers, diving them results in an integer. That works out great since dog years are always expressed as integers.

The next line combines two strings and dog_years into larger string and displays it.

Calculating seconds

The user's age, in seconds, is calculated and printed in the two following lines:

 seconds = age * 365 * 24 * 60 * 60 print "But you're also over", seconds, "seconds old." 

Since there are 365 days in a year, 24 hours in a day, 60 minutes in an hour, and 60 seconds in a minute, age is multiplied by the product of 365 * 24 * 60 * 60. This value is assigned to seconds. The next line combines two strings and seconds into a larger string and displays it.

Printing name Five Times

The program displays the user's name five times in a row using string repetition:

 called = name * 5 print "\nIf a small child were trying to get your attention, " \        "your name would become:" print called 

The variable called is assigned the value of name, repeated five times. Then, a message is printed followed by called.

Calculating moon_weight and sun_weight

The next four lines calculate and display the user's weight on the moon and sun:

 moon_weight = weight / 6.0 print "\nDid you know that on the moon you would weigh only", moon_weight, "pounds?" sun_weight = weight * 27.1 print "But on the sun, you'd weigh", sun_weight, "(but, ah... not for long)." 

Since the moon has one-sixth the gravitational pull of the earth, moon_weight is assigned the value of weight divided by 6.0. I use a floating-point number so that the result is a more accurate floating-point number instead of an integer.

Since the gravitational force on the sun is about 27.1 times stronger than it is here on earth, I multiply weight by 27.1 and assign the result to sun_weight. Again, since 27.1 is a floating-point number, sun_weight will be a float too.

The next two lines print out messages telling the user about his or her new weights.

Waiting for the User

The last statement waits for the user to press the Enter key:

 raw_input("\n\nPress the enter key to exit.") 




Python Programming for the Absolute Beginner
Python Programming for the Absolute Beginner, 3rd Edition
ISBN: 1435455002
EAN: 2147483647
Year: 2003
Pages: 194

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net