![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png) aranthe) wrote in
aranthe) wrote in ![[community profile]](https://www.dreamwidth.org/img/silk/identity/community.png) intro_to_cs2010-01-15 01:28 pm
intro_to_cs2010-01-15 01:28 pmif Exercises in Programming Python, "Conditional Statements"
I've annotated the code fairly liberally, but if you have any questions, just ask. I'll be happy to explain anything that seems confusing.
I just realized that in an effort to make Exercise 1 less boring, I included some extra print statements that weren't required. To avoid confusion, I've replaced my first solution with a simplified one. For those who are interested, I'll post the embroidered one in a reply to this thread.
Ex 1: Write a password guessing program to keep track of how many times the user has entered the password wrong. If it is more than 3 times, print "You have been denied access." and terminate the program. If the password is correct, print "You have successfully logged in." and terminate the program.
Note: I used a baseball analogy. For strike 3, it prints what you would expect. For success, it prints "That's a homer!"
# login_simplified.py >> if exercise 1: Password guessing (login) program.
# Initialize a variables to hold password, counter and message prefix.
password = 'mellon'
strikes = 0
# The user gets 3 tries.
while strikes < 3:
    # Request the user's password.
    strike = raw_input('Enter your password: ')
    # Check the input against the stored password.
    if strike == password:
        # If it matches, 
        print "\nThat's a homer!"     # print success
        strikes = 3                   # set counter to 3 to end while
    else:
        # If it doesn't match,
        strikes += 1                  # increment the counter
        # Is this the last one?
        if strikes == 3: 
            print "Steee-rike 3! You're out!"
Ex 2: Write a program that asks for two numbers. If the sum of the numbers is greater than 100, print "That is a big number" and terminate the program.
# sum.py >> Exercise 2: Sum two numbers; check to see if they are greater than 100.
# Get the user's input and convert it. I converted to integers, 
# but you could convert to floats to allow for decimals if you like.
num1 = int(raw_input('Enter your first number: '))
num2 = int(raw_input('Enter your second number: '))
# Add the numbers and see if their sum is greater than 100.
if (num1 + num2) > 100:
    # If so, print the message. (Yeah, kinda boring.)
    print "That's a big number!"
Ex 3: Write a program that asks the user their name. If they enter your name, say "That is a nice name." If they enter "John Cleese" or "Michael Palin", tell them how you feel about them, otherwise tell them "You have a nice name."
# test_name.py >> Exercise 3: Get user name and test it against a few values.
# Initialize variables to hold the comparison values.
# Note: Setting all lowercase makes it easy
# to normalize the user's input for cleaner comparison.
my_name = 'rumplestiltskin'
python1 = 'john cleese'
python2 = 'michael palin'
# Obtain the user's name.
name = raw_input('Enter your name: ')
# Check it against the stored values.
# Convert user input to lower case for comparison. 
# This eliminates the problem of case variations.
if name.lower() == my_name:
    print "Ooh! Can you spin straw into gold?"
# Compound condition to check for 2 Pythons.
elif name.lower() == python1 or name.lower() == python2:
    print 'And now for something completely different...'
# Everyone else.
else:
    print 'You have a nice name.'





no subject
Additional thought on that last one: It's more efficient if you convert the input to lowercase before you store it to name. This saves repetition of the operation in the
if/elif/elsestructure.no subject
(Anonymous) 2011-09-29 08:27 pm (UTC)(link)