#WSQ06

 

 

 

 

 

 

This code required to import a module, but what is module?  you might ask. The answer of that question is inside this page: https://docs.python.org/2/tutorial/modules.html

Basically a module is script or python file that cointains several functions and statements. You can import modules to the main module of python and you can also create your own. Most of the functions and statements we need by now, already exists within the python modules. 

 

But let´s get back to the assignment; for this program we need to ask the user for a random number until he guesses the exact same number that the our code has chosen randomly.

 

 

-First we need to import the random module, with this module we can use a function that selects a random number of a certain range.

import random

num=(random.randint(1,100))

 

-After asking the user to try to guess the number, we need to impliment a while loop so the user can  have many tries until he guesses the number. 

print (“I have a number chosen between 1 and 100”)
print (“Try to guess that number”)
countg=”guesses to get the right number”
guess=0
count=0
while int (guess) != int (num):
guess=input()
count= int(count)+1

-This code means that when the user inputs the right number the loop will end and the program will be over.

 

-But that is not it, we also need to tell the user if his guess is lower or higher than the actual number. To do this I used If and else conditions. 

if int(guess) > int(num):
print ( guess,”is too high, try again”)
if int(guess) < int(num):
print(guess,”is too low, try again”)

print(“You guessed the number!”)
print(“It took you”,count,countg)     

-I also used a counter

random
guess

counting how many times the user tried to guess the right number

The final code is this:

random

The program looks like this:

guess

 

And that´s it.

 

 

CC BY-SA 4.0 #WSQ06 by alextenablog is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.