miércoles, 22 de febrero de 2017

Pick a number

Pick a number
For our #WSQ03 task we were asked for a program that picks a random integer between 1 and 100, the it prompts the user for a guess of the value, and it will hint "too low" or "too high". The program will continue to run until the user guesses the integer. As an extra credit we were asked to show the user how many tries it took him to guess the number.

1.- For this exercise we need to import the "random library" by typing "import random", with these we can use the random function just like this:

import random
n=random.randint(1,100)   notice how the function has to be written. 
print("Shhhhh. the number is",n)  this is for you to know what the number is, so it will make it easier test your code.

2.- Ask the user for an integer:
print ("I have choosen a random number between 1 and 100, can you guess it?")
guess=input("Please write a number")
guess=int(guess)
count=1  since we want to show the user how many guesses it took him to guess the answer , we need to declare a variable and give it the value of one.

3.- Now we have to make a loop function to make it go over the process over and over again until the user guesses the number:
while n!=guess: this tells the program to keep making a cycle while n is different from the guess
    count=count+1this is the counter
    if guess>n:
        print ("too big")
    else:
        print ("too small")
    print("Keep trying")
    guess=int(input("Try another number"),)

print("it took you",count, "guesses")

No hay comentarios:

Publicar un comentario