Login

Back

Guessing game!

Q1. Type this code in and run it:

1    import random
2    print("\tWelcome to Guess the number!")
3    print("\nI've thought of a number between 1 and 100.")
4    print("How many attempts do you need to guess it?")
5

6    #Get a random number
7    the_number = random.randint(1,100)
8    guess = int(input("Take a guess >>> "))
9    attempts = 1
10

11  #guessing loop
12  while guess!=the_number:
11       if guess > the_number:
13            print("Lower ....")
14       else:
15            print("Higher ....")
16       guess = int(input("Take a guess >>> "))
17       attempts += 1
18
19  print("You guessed it! The number was",the_number,end=".")
20  print("\nYou took",attempts,"attempts.")
21 input("\n\nPress <ENTER> to quit >>> ")

Q2. How would you place lines 2 - 5 with single print instruction?
Q3.
To make the game easier for younger children, you could reduce the range of numbers that might be selected. How could you do this?
Q4.
It has been decided to select only odd numbers. How would you do this? (HINT: you need to look on the Internet for the Python documentation for the random module and find a suitable instruction. Don't forget to test it works!)
Q5. 
It has been decided to select only even numbers between 2 and 200 inclusive. How would you do this? (HINT: you need to look on the Internet for the Python documentation for the random module and find a suitable instruction. Don't forget to test it works as you intended!)
Q6.
Write a program that outputs a random even number between 30 and 50. After each number has been outputted, your user should be asked if they want another random number, and to press capital Y for yes and any other key for no. Random numbers should be outputted until any other key apart from Y is pressed.
Q7. Users are allowed to press Y or y for yes. Modify your program to achieve this.

Back