import random
def generate_expression():
operators = ['+', '-', '*', '/'] #a
operator = random.choice(operators)
num1 = random.randint(1, 100) #b
num2 = random.randint(1, 100) #c
# 避免出现除法运算时的除数为0的情况
if operator == '/' and num2 == 0: #f
num2 = random.randint(1, 100) #d
expression = f"{num1} {operator} {num2}" #g
return expression
if __name__ == "__main__":
for _ in range(5): # 生成五个表达式 #e
print(generate_expression()) #y
程序流程图:
PAD图: