#WSQ06 – Pick a Number

Time for “While” loops, here’s what we need to do:

  • Write a program that picks a random integer in the range of 1 to 100.
  • It then prompts the user for a guess of the value, with hints of ’too high’ or ’too low’ from the program.
  • The program continues to run until the user guesses the integer. You could do something extra here including telling there user how many guesses they had to make to get the right answer.

Let’s start:

1WSQ06

You strictly need to import the random library, don’t forget the “import random” sentence or your program isn’t going to work.

Our random number (“r”) will be composed by our number (num), multiplied by the random number the program generates, plus one.

2WSQ06

Time for the real thing. You just have to open a while loop that consists on repeating the same action as long as our guessing number (num) reaches equality with our random number (r).

When num = r the “print” line at the bottom will run, telling us that we guessed the number.

Let’s run the program:

RunWSQ06

Here’s the full program:

FullWSQ06

 

Not the most easiest thing in the world, but nothing we can’t do.

CC BY-SA 4.0 #WSQ06 – Pick a Number by manuelmendezhijar is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.