# 1.1 print student name
print("Student Name: Chiu Yin Shan")
# 1.2 Randomly generate the answer between 1 and 100
import random
answer = random.randint(1, 100)
#print(answer)
lower_bound = 1
upper_bound = 100
# 1.3 print range as instruction
print(f"Guess a number between {lower_bound} and {upper_bound}.")
# 1.4 check whether is number
def is_number(user_input):
"""Check if the input is a valid number (integer)."""
try:
return int(user_input)
except ValueError:
return None
# 1.5 loop & if-else to check
while True:
# Ask user for input and validate
user_input = input(f"Enter a number ({lower_bound}-{upper_bound}): ")
number = is_number(user_input)
# Check if input is a valid number
if number is None:
print("Invalid input. Please enter an integer.")
continue
# 1.5.1 out of range
if number < lower_bound or number > upper_bound:
print(f"Please enter a number within {lower_bound}-{upper_bound}.")
continue
# Compare with the answer
if number == answer:
print(f"Congratulations! The correct answer is {answer}.")
break
elif number < answer:
lower_bound = number
else:
upper_bound = number
print(f"Sorry, good try! The new range is {lower_bound}-{upper_bound}. Try again!")