[personal profile] aranthe posting in [community profile] intro_to_cs

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.'

Re: Ex 1 with counted strikes

Date: 2010-11-22 01:44 am (UTC)
From: (Anonymous)
. One way to do it is to use a While True and break statements.

Assuming that the correct password is “python1” or “python2”, write a password program to keep track of how many times the user has entered the password wrong. If it is more than 10 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. Also if the user enters “bye” or “quit” any time during the program, print the message “You want to quit the program. Nice day.” and terminate the program.

Date: 2010-01-16 10:34 pm (UTC)
winterthunder: (Default)
From: [personal profile] winterthunder
Thank you!

wnODHgscnPmi

Date: 2012-05-03 09:17 am (UTC)
From: (Anonymous)
This is just the pferect answer for all forum members

Date: 2011-09-29 08:27 pm (UTC)
From: (Anonymous)
u r gay

Profile

Introduction to Computer Science

July 2010

S M T W T F S
    123
45678910
11121314151617
18192021222324
2526272829 3031

Style Credit

Expand Cut Tags

No cut tags
Page generated Oct. 21st, 2017 10:56 pm
Powered by Dreamwidth Studios