-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguess a number
31 lines (25 loc) · 1.42 KB
/
guess a number
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
# Exam Question:
# You are tasked with developing a simple number-guessing game. The game should have the following features:
# 1. The user is asked to input the upper limit of a range (must be a number greater than 0). If the input is invalid, the program should exit with an appropriate error message.
# 2. The program generates a random number between 0 and the user-specified upper limit.
# 3. The user guesses the random number, and the program provides feedback after each guess:
# If the guess is too high, display a message indicating the guess is "above the number."
# If the guess is too low, display a message indicating the guess is "below the number."
# If the guess is correct, display a congratulatory message and the total number of attempts.
# 4. If the user's guess is invalid (not a number), the program should prompt them to input a valid number without exiting.
# Write a Python program that meets these requirements.
# Note: Ensure the program uses a while loop for repeated guessing and an if-elif-else structure for feedback logic.
import random
count = 0
while True:
num = int(input('enter a number between 1 to 3'))
rand = random.randint(1,3)
if num < rand:
print('your answer is lesser than the correct answer')
count += 1
elif num > rand:
print('your answer is greater than the correct answer')
count += 1
else:
print('correct answer ', count)
break