Showing posts with label conditional. Show all posts
Showing posts with label conditional. Show all posts

Thursday, March 27, 2014

Exercise 8: While loops (rock, paper, scissors)

Exercise

Make a two-player Rock-Paper-Scissors game. (Hint: Ask for player plays (using input), compare them, print out a message of congratulations to the winner, and ask if the players want to start a new game)
Remember the rules:
  • Rock beats scissors
  • Scissors beats paper
  • Paper beats rock

Discussion

Concepts for this week:
  • While loops
  • Break statements

While loops

We have already discussed for loops, or loops that look sequentially (one by one) at elements in a list. There is a second type of loop that works in a slightly different way called a while loop.
The idea is simple: while a certain condition is True, keep doing something. For example:
a = 5
while (a > 0):
    print(a)
    a -= 1
The output of this code segment is:
5
4
3
2
1
A particularly useful way to use while loops is checking user input for correctness. For example:
quit = input('Type "enter" to quit:' )
while quit != "enter":
    quit = input('Type "enter" to quit:' )
The uses for this are infinite, and can (and should!) be combined with conditionals to yield the most efficient results.

Break statements

A break statement stops the execution of a loop before the original condition is met. While the use of a break statement will often start an argument about good coding practices, sometimes it is useful.
For example:
while True: 
    usr_command = input("Enter your command: ")
    if usr_command == "quit":
        break
    else: 
        print("You typed " + usr_command)
In this case, the break statement is used to break off the "infinite while loop" that we have constructed with the while True statement.

Happy coding!
Forgot how to submit exercises?

Wednesday, March 19, 2014

SOLUTION Exercise 6: String lists

Exercise

Ask the user for a string and print out whether this string is a palindrome or not. (A palindrome is a string that reads the same forwards and backwards.)

Sample solution

Saturday, February 15, 2014

SOLUTION Exercise 2: conditionals

Full exercise post

Ask the user for a number. Depending on whether the number is even or odd, print out an appropriate message to the user. Hint: how does an even / odd number react differently when divided by 2?

Extras:

  • If the number is a multiple of 4, print out a different message.
  • Ask the user for two numbers: one number to check (call it num) and one number to divide by (check). If check divides evenly into num, tell that to the user. If not, print a different appropriate message.

  • Sample solution

    There are many ways of doing the exercise, so I am posting a few sample solutions. The very basics:

    And something that looks slightly more complex (but is just a more complicated conditional):


    Wednesday, February 5, 2014

    Exercise 2: conditionals

    Again, the exercise comes first (with a few extras if you want the extra challenge or want to spend more time), followed by a discussion. Enjoy!

    Exercise

    Ask the user for a number. Depending on whether the number is even or odd, print out an appropriate message to the user. Hint: how does an even / odd number react differently when divided by 2?

    Extras:
    1. If the number is a multiple of 4, print out a different message. 
    2. Ask the user for two numbers: one number to check (call it num) and one number to divide by (check). If check divides evenly into num, tell that to the user. If not, print a different appropriate message.

    Discussion

    Concepts for this week:
    • Modular arithmetic (the modulus operator)
    • Conditionals (if statements)
    • Checking equality

    Modular arithmetic (the modulus operator)

    We have been doing arithmetic (addition, subtraction, multiplication, division) since elementary school, and often it is useful for us to find not the answer to a division problem but the remainder when we do a division operation. This operation is called the "modulus operation." For example, when I divide 5 by 3, the remainder is 2, and the sentence reads like this: "5 modulo 3 is 2."

    In the Python shell:
    ```
    >>> 5 % 3
    2
    >>> 6 % 3
    0
    >>> 7 % 3
    1
    ```
    The % sign is exactly the modulus operator.

    Conditionals

    When a computer (or a program) needs to decide something, it checks whether some condition is satisfied, which is where the term conditional comes from. Conditionals are a fancy way of saying "if statements". If Michele was born in New York, she has an American passport. That statement is a conditional (if statement) that in this case is true. In Python this works the same way:

    if age > 17: 
        print("can see a rated R movie")
    elif age < 17 and age > 12:
        print("can see a rated PG-13 movie")
    else: 
        print("can only see rated PG movies")
    
    
    When the program gets to the if statement, it will check the value of the variable called age against all of the conditions, in order, and will print something to the screen accordingly. Note that elif is a portmanteau of "else" and "if". So if the variable age holds the value 15, the statement "can see a rated PG-13 movie" will be printed to the screen.

    Note how the statement elif age < 17 and age > 12 has the statement and - you can use or and not in the same way. Understanding a bit about logic and how it works, or being able to rationally think about logic will help you get the conditions right - oh, and a lot of practice.

    Links about conditionals:



    Checking for equality (and comparators in general)

    A fundamental thing you want to do with your program is check whether some number is equal to another. Say the user tells you how many questions they answered incorrectly on a practice exam, and depending on the number of correctly-answered questions, you can suggest a specific course of action. For integers, strings, floats, and many other variable types, this is done with a simple syntax: ==. To explicitly check inequality, use !=.

    if a == 3: 
        print("the variable has the value 3")
    elif a != 3:
        print("the variable does not have the value 3")
    
    
    Notice how in this example, the condition is redundant. In the first condition we are checking whether the variable a has the value 3 and in the second, we are checking whether a does NOT have the value 3. However, if the first condition is not true (a is in fact not 3), then the second condition is by definition true. So a more efficient way to write the above conditional is like this:

    if a == 3: 
        print("the variable has the value 3")
    else:
        print("the variable does not have the value 3")
    
    
    The same equality / inequality comparisons work for strings.

    Links about equality and comparators:
    Good luck and enjoy!
    Forgot how to submit an exercise?