-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomGuess.py
More file actions
30 lines (21 loc) · 1.58 KB
/
Copy pathRandomGuess.py
File metadata and controls
30 lines (21 loc) · 1.58 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
# This program generates a random number in the range of 1 through 15.
# Which will ask the user to guess what the number is.
def main(): #This line defines a main() function.
user_guess = 99 #user_guess equivalent to 99.
Number = randomNum() #Number is equivalent to randomNum() function in which it calls for it.
while user_guess!= 0: #The loop runs while the user inputs a non-zero number from 1 to 15.
user_guess = int(input('Enter a number between 1 and 15, or 0 to quit: '))
#Depending on the user's input, the program either...
if user_guess > Number: print('Too high, try again')
#prints "Too high, try again" if the user's guess is greater than Number.
elif user_guess < Number: print('Too low, try again')
#prints "Too low, try again" if the user's guess is less than Number.
elif user_guess == Number: print('Congratulations! You guessed the right number!')
#prints "Congratulations! You guessed the right number!" if the user's guess is equal to Number.
if user_guess == 0: print('Thanks for playing!')
#If the user inputs 0, the program prints "Thanks for playing!" and ends the game.
def randomNum(): #Calls the randomNum() function where it generates random integers.
import random #Imports the random function.
random=random.randint(1,15) #randomNum() creates a random integer between 1 and 15 using random.randint() from the random module.
return (random) #The random integer is then returned to the main() function.
main() #Calls the main() function first.