Showing posts with label input. Show all posts
Showing posts with label input. Show all posts

Friday, April 11, 2014

SOLUTION Exercise 9: Randomness, a guessing game

I apologize for being a day late on the solution / exercise for this week, I hope you'll enjoy it anyway!

Exercise


Generate a random number between 1 and 9 (including 1 and 9). Ask the user to guess the number, then tell them whether they guessed too low, too high, or exactly right. (Hint: remember to use the user input lessons from the very first exercise)

Extras:



  • Keep the game going until the user types "exit"
  • Keep track of how many guesses the user has taken, and when the game ends, print this out.

  • Sample solution


    Great example of a solution including both extras!

    Wednesday, April 2, 2014

    Exercise 9: Randomness, a guessing game

    Exercise

    Generate a random number between 1 and 9 (including 1 and 9). Ask the user to guess the number, then tell them whether they guessed too low, too high, or exactly right. (_Hint: remember to use the user input lessons from the very first exercise
    Extras:
    • Keep the game going until the user types "exit"
    • Keep track of how many guesses the user has taken, and when the game ends, print this out.

    Discussion

    Concepts for this week:
    • Modules
    • Random numbers
    • User input

    Random Numbers (and Modules)

    This is your first exposure to using Python code that somebody else wrote. In Python, these formally-distributed code packages are called modules. The thing we want from a module in this exercise is the ability to generate random numbers. This comes from the random module.
    To use a module, at the top of your file, type
    import random
    This means you are allowing your Python program to use a module called random in the rest of your code.
    To use it (and generate a random integer), now type:
    a = random.randint(2, 6)
    Once you run this program, the variable a will have a random integer that the computer made for you, between 2 and 6 (including 2, not including 6).
    There are many ways you can generate random numbers - integers, decimals, and much more. The Python documentation has much more detailed information about what is possible from the random module.

    User input

    We covered all you need to know in the first exercise of this blog!

    Happy Coding!

    Forgot how to submit exercises?

    SOLUTION Exercise 8: rock, paper, scissors

    I was slightly disappointed by how few people attempted the exercise this week - try it, it's a fun one, and look at the sample solutions if you are stuck. I was extremely happy with the solutions I did see! Feel free to reach out to me personally if you want more personalized help.

    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

    Sample solution

    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?

    SOLUTION Exercise 1: inputs and strings

    Great job this week with finishing the first weekly problem! Because I cannot include every single submission I get, I will choose one or two that are example answers and include those within each post.

    Exercise (click to see the full post and explanation)

    Create a program that asks the user to enter their name and their age. Print out a message addressed to them that tells them the year that they will turn 100 years old.

    Sample solution

    Note how a sample correct solution makes liberal use of the int and str functions - changing from one type to another.

    Wednesday, January 29, 2014

    Exercise 1: input and strings

    Calibrating the exercises to the audience is going to be a challenging task, so I ask you to bear with me if the exercises are too easy or too hard. Every week there will be a poll you can click on to discuss whether the exercise is too easy or too hard and hopefully in a few weeks, I'll get the level right. Let's get to it! I will start with the exercise and include a discussion later, in case you want the extra challenge.

    Exercise(s)

    Create a program that asks the user to enter their name and their age. Print out a message addressed to them that tells them the year that they will turn 100 years old.
    Extras:
    1. Add on to the previous program by asking the user for another number and printing out that many copies of the previous message. (Hint: order of operations exists in Python)
    2. Print out that many copies of the previous message on separate lines. (Hint: the string "\n" is the same as pressing the ENTER button)

    Discussion

    Concepts for this week:
    • Getting user input
    • Manipulating strings (a few ways)

    User input in Python


    To get user input in Python (3), the command you use is input(). Store the result in a variable, and use it to your heart's content. Remember that the result you get from the user will be a string, even if they enter a number.
    For example,
    name = input("Give me your name: ")
    print("Your name is " + name)
    What this will print in the terminal (or the shell, whatever you are running Python in) will be:
    >>> Give me your name: Michele
    Your name is Michele
    What happens at the end of input() is that it waits for the user to type something and press ENTER. Only after the user presses ENTER does the program continue.

    Manipulating strings (a few ways)


    What you get from the input() function is a string. What can you do with it?
    1. Make the string into a number. Let's say you are 100% positive that the user entered a number. You can turn the string into an integer with the function int(). (In a later exercise or two or three there will be questions about what to do when the user does NOT enter a number and you try to do this; for now don't worry about that problem). Here is what this looks like:
      age = input("Enter your age: ")
      age = int(age)
      (or, if you want to be more compact with your code)
      age = int(input("Enter your age: "))
      In both cases, age will hold a variable that is an integer, and now you can do math with it.
      (Note, you can also turn integers into strings exactly in the opposite way, using the str() function)
    2. Doing math with strings. What do I mean by that? I mean, if I want to combine (concatenate is the computer science word for this) strings, all I need to do is add them:
      print("Were" + "wolf")
      print("Door" + "man")
      print("4" + "chan")
      print(str(4) + "chan")
      The same works for multiplication, but division and subtraction do not work like this.

    Forgot how to submit an exercise?