def check_num(num):
try:
return float(num)
except ValueError:
print("input error" + str(num))
exit()
def check_op(operator):
if operator in ["+","-","*","/"]:
return True
else:
print("Wrong operator")
return False
#try:
#a = float(a)
#b = float(b)
# if op in ["+","-","*","/"]:
#print("op is right")
def calc(a,b,op):
a = check_num(a)
b = check_num(b)
print(a,b)
if check_op(op):
if op == '+':
result = a + b
if op == '-':
result = a - b
if op == '*':
result = a * b
if op == '/':
if b == 0:
print("** b can not be zero **")
else :
result = a/b
return result
result = 0
#else:
#print("wrong operator")
#except:
#print("Error")
#except ZeroDivisionError :
#print("b can't be zero")
# print("data type error")
#exp = '1+1'
exp = '1 + 1'
exp = exp.split()
print(exp)
#a = -2
#b = 9
#op = '/'
while (len(exp) >=3 and len(exp)%2):
a = exp[0]
b = exp[2]
op = exp[1]
result = calc(a,b,op)
del exp[:2]
print(exp)
exp[0] = result
print ("expression result is:" + str(result))
student_Name = "Lam Hiu Wing"
print (f"student_Name:Lam Hiu Wing")
#
import random
target = random.randint(1,100)
#min = 1
#max = 100
count = 0
#
def get_valid_input():
while True:
guess = input("please input a number between 1 and 100:")
if not guess:
print("NO INPUT!!!Please input a number.")
continue
try:
guess = int(guess)
if 1 <= guess <= 100:
return guess
else:
print("Sorry, good try. The new range is {new range}. Please try again!")
except ValueError:
print("INVALID INPUT!!!Please input a number")
#
def check_guess(guess, target):
if guess == target:
return True, f"Congratulations, The correct answer is {target}."
else:
return False
#
while True:
count += 1
print(f"count:{count}")
guess = get_valid_input()
is_correct, message = check_guess (guess,target)
print(message)
if is_correct:
break
print(f"game over! total guesses:(count)")
#
#def get_valid_input():
#while True:
# guess = input("please input a number between 1 and 100:")
if not guess:
print("NO INPUT!!!Please input a number.")
continue
try:
guess = int(guess)
if 1 <= guess <= 100:
return guess
else:
print("Please enter a number within the range 1 to 100")
except ValueError:
print("INVALID INPUT!!!Please input a number")
#
def check_guess(guess, target):
if guess == target:
return True, f"Congratulations, The correct answer is {target}."
else:
return False, f"sorry, good try. Please try again!"
student_Name = "Lam Hiu Wing"
print (f"student_Name:Lam Hiu Wing")
#
import random
target = random.randint(1,100)
min_range = 1
max_range = 100
count = 0
#
#
#
#
while True:
count += 1
print(f"count:{count}")
print(f"please input a number from {min_range} to {max_range}:")
guess = input()
if not guess:
print("NO INPUT!!!Please input a number.")
continue
try:
guess = int (guess)
except ValueError:
print("INVALID INPUT!!!Please input a number")
continue
if guess < min_range or guess > max_range:
print ("Please enter a number within the range {min_range} to {max_range}.")
continue
if guess == target:
print("Congratulations, The correct answer is {target}.")
break
else:
if guess < target:
min_range = guess
else:
max_range = guess
print (f"sorry, good try. the new range is {min_range} to {max_range}. Please try again!")
#is_correct, message = check_guess (guess,target)
#print(message)
#if is_correct:
#break
#print(f"game over! total guesses:{count}")