-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandom_Numbers_Game_Shakeel.py
More file actions
63 lines (48 loc) · 2.66 KB
/
Copy pathRandom_Numbers_Game_Shakeel.py
File metadata and controls
63 lines (48 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# The Guessing Game Project - Shakeel Abbas
#'\n' creates a new line
####initialisation
# need to import the random function in order to generate the random number later
import random
#setting the range of numbers to be used in The Guessing Game
rangeOfNumbers = range(1, 11)
#to see the numbers are in the correct range - remove the 2 comment lines below
# for n in rangeOfNumbers:
# print(n)
# this pulls a random number chosen from rangeOfNumbers and holds it for the duration of the program
randomNumber = random.choice(rangeOfNumbers)
#initialising the values for numberOfGuesses and User Guess
numberOfGuesses = 0
Guess = 0
###processing
#opening menu item's displayed with range of numbers in a string to easily change the game as per requirements
#1
print("Welcome to The Guessing Game!")
#2
print("\nHow the game works:\n\n- Guess a number in the following " + str(rangeOfNumbers[0:-1]) + "\n- You have only 3 tries to Guess the number and win\n- You will get a clue after each wrong Guess whether the number was higher/lower")
#3
input('\nPress any key to continue...\n')
###decisions
#setting the number of guesses and outcomes for different results with a while loop
#program will keep running until 3 guesses are made or the correct number is guessed
while numberOfGuesses < 3:
#input area for the user to guess
Guess = int(input("\nWhat is your Guess? "))
numberOfGuesses += 1 #count of the user guesses
if Guess < 1 or Guess > 10: #how to deal with a value error
print("Invalid integer. The number must be in the range mentioned above. Please try again.")
numberOfGuesses += -1
elif Guess > randomNumber: #if guess is higher than the random number, display this message
print("Uh oh, The number is lower than that.")
elif Guess < randomNumber: #if guess is lower than the random number, display this message
print("Uh oh, The number is higher than that.")
if Guess == randomNumber: # if guess is correct, break the while loop
break
#final output - Possible Correct/Incorrect Guess outcomes with string of number of guesses and random number for ease of changing the program
#Correct Guess Final Message
if Guess == randomNumber:
print("\nCongratulations you have guessed the correct number in just " + str(numberOfGuesses) + " tries!! \nWell done!")
print('The End, Thanks for playing The Guessing Game!')
#Incorrect Guess Final Message
else:
print('\nOh no, You ran out of guesses and did not guess the number, the number was: ' + str(randomNumber) + '. Better luck next time.')
print('\nThanks for playing The Guessing Game!')