from random import randint
from random import? choice
def exam():
? ? '用于出題嚷硫,用戶作答'
? ? # 隨機(jī)生成兩個(gè)整數(shù)
? ? nums = [randint(1, 100) for i in range(2)]
? ? nums.sort(reverse=True)? # 降序排列
? ? # nums.reverse()
? ? # 隨機(jī)選擇加減法
? ? op = choice('+-')
? ? # 計(jì)算出標(biāo)準(zhǔn)答案
? ? if op == '+':
? ? ? ? result = nums[0] + nums[1]
? ? else:
? ? ? ? result = nums[0] - nums[1]
? ? # 用戶作答卫漫,判斷正誤
? ? prompt = f'{nums[0]} {op} {nums[1]} = '
? ? n = 0
? ? while n <3 :
? ? ? ? try:
? ? ? ? ? ? answer = int(input(prompt))
? ? ? ? except ValueError:
? ? ? ? ? ? print('無(wú)效輸入,重輸')
? ? ? ? ? ? continue
? ? ? ? if answer == result:
? ? ? ? ? ? print('你真棒!!!')
? ? ? ? ? ? break
? ? ? ? else:
? ? ? ? ? ? print('不對(duì)喲!!!')
? ? ? ? n+= 1
? ? else:
? ? ? ? print(f'corect answer is :\n{prompt}{result}')
def main():
? ? '主程序代碼邏輯'
? ? while 1:
? ? ? ? exam()
? ? ? ? try:
? ? ? ? ? ? yn = input('Continue(Y/n)? ').strip()[0]? # 取出用戶輸入的第一個(gè)非空字符
? ? ? ? except (KeyboardInterrupt,EOFError):
? ? ? ? ? ? print('\nBye-bye')
? ? ? ? ? ? break
? ? ? ? except IndexError:
? ? ? ? ? ? continue
? ? ? ? if yn in 'nN':
? ? ? ? ? ? print('\nBye-bye')
? ? ? ? ? ? break
if __name__ == '__main__':
? ? main()