#WSQ06

Pick a Number

Write a program that picks a random integer in the range of 1 to 100.

There are different ways to make that happen, you choose which one works best for you.

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.

You might want to check that your program doesn’t always use the same random number is chosen and you should also split your problem solving into parts. Perhaps only generate the random number and print that as a first step.

import random

value = random.randint(1,100)

tries =0

num = int(input(“Guess a number from 1 to 100: “))

while num != value:

    if num > value:

        print (num, “is too high.”)

        num = int(input(“Try again: “))

        tries = tries + 1

    elif num < value:

        print (num, “is too low.”)

        num= int(input(“Try again: “))

        tries = tries + 1

tries = tries + 1

print (“Congratulations!, The right number is”,value)

print (“It took you” ,tries, “tries to win”)

CC BY 4.0 #WSQ06 by Luis Vargas is licensed under a Creative Commons Attribution 4.0 International License.

Comments are closed.