任务一:
def calc(a, b, op): if op == "+": return add(a, b) elif op == "-": return sub(a, b) elif op == '*': return mul(a, b) elif op == "/": if b!= 0: return div(a, b) else: return '除数不能为0' else: print("输入的运算符不正确!!") def add(a, b): return a + b def sub(a, b): return a - b def mul(a, b): return a * b def div(a, b): return a / b if __name__ == '__main__': a = int(input('请输入第一个整数:')) b = int(input('请输入第二个整数:')) op = input('请输入运算符:') print(calc(a, b, op))
任务二:
import random def guess(num,guess_num): if num == guess_num: return 0 elif guess_num>num: return 1 else: return -1 num = random.randint(1, 100) for i in range(10): guess_num = int(input('我心里有个【1-100】的整数请你猜一下:')) result = guess(num, guess_num) if result == 0: print('猜对了') break elif result ==1: print('大了') else: print('小了') else: print('真笨,10次都没有猜中!')