Pick a Number

This was the assignment we had to do this week:

What to Do

https://www.flickr.com/photos/dasprid/8147975983/

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.

Example Run

I have a number chosen between 1 and 100.
Please guess a number between 1 and 100:  50
I’m sorry but 50 is too high, try again: 25
I’m sorry but 25 is too low, try again: 42
You got it! The right answer is indeed 42.
You made 3 guesses to get the right number.

 

This is the way in which I solved the problem:

This week I tried to use the PyCharm CE software instead of Atom, because PyCharm CE provides the ability to run the program directly on the app.

So the only prerequirement I needed was PyCharm CE.

What I’ve learned from the past assignments was that the best way to program is to write little pieces of code and then check if it works or not.

So I did the following:

  1. On the web, I’ve searched for a command that enables the computer to select a random integer number between 1 and 100. This the code that I found and that worked for me:
    import random
    
    n=(random.randint(1,101))
    
    The import part is here to insert a range from 1 to 101 in the code, because it is the range is exclusive.
  2. After that I wanted to give the user the option to guess a random number between that range.
    print("Please guess a number between 1 and 100.")
    x =int(input("Enter the number: "))
  3. Then I’ve tried to insert a loop function with the command „while“.
    while  n !=number:

    I’ve used this to assign the computer to process the loop when the guessed number does not equal the random number.

  4. Next, I told the computer that there are two conditions. The first one when the guessed number is higher than the random number and vice versa. For the later, I only used the function else, which was the easiest way for me.
    if n > number:
    
    else:
  5. Then I needed to define what the computer is to print if one of those conditions is true:
    
    print("I'm sorry, but" , number, "is too low. Please try again.")
    
    number =int(input("Enter the number: "))
    
    or:
    
    print("I'm sorry, but" , number, "is too high. Please try again.")
    
    number =int(input("Enter the number: "))

6. After that I wanted to integrate a function that provide the number of tries to the user. For that, first I inserted the following assignment in the loop:

count = 1

7. In both conditions I added the following for the tries to be counted:

count = count + 1

8. After that, I added the sentence for the result. While I wrote the sentence I realized that it could be that the user guesses the answer at the first time. So I wanted to integrate two conditions in order to match to the grammatics with the form of the singular and plural of the word „time“ or „times“.

if count == 1:
    print("You've got it!", number, "is the right number! You've tried 1 time!")

else:
    print("Not bad, you've got it!", number, "is the right number! You've tried", count, "times!")

9. That was it. The whole code looks like that and it works!

pickanumber

 


UPDATE!

In class today we’ve learned that the assignment random.randint isn’t enough to produce random numbers, because it is possible that the most probably the same sequence of random numbers is going to be produced in the next run. So, I’ve added a seed that is based on time to create pseudo random numbers from 1 to 100.

 

import random
from datetime import time

random.seed(time)
n =(random.randint(1, 100))