Showing posts with label random. Show all posts
Showing posts with label random. Show all posts

Wednesday, May 28, 2014

Exercise 15: A Password Generator

Exercise

Write a password generator in Python. Be creative with how you generate passwords - strong passwords have a mix of lowercase letters, uppercase letters, numbers, and symbols. The passwords should be random, generating a new password every time the user asks for a new password. Include your run-time code in a main method.
Extra:
  • Ask the user how strong they want their password to be. For weak passwords, pick a word or two from a list.

Discussion

There are no new topics this week, but you will need to use Python's random module, described in this post.

Happy coding!

Explore away!
Forgot how to submit exercises?

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?