以下代码有问题,过段时间来学习,看下什么原因:
import random '''a=random.randint(1,3)''' b=random.randint(1,100) c=random.randint(1,100) if a==1: print(b+c) answer=input('您的答案是') j=b+c if answer==j: print("right") else: print("sorry,your answe is wrong") break '''if a==2: print(b*c) answer=input('您的答案是') j=b*c if answer==j: print("right") else: print("sorry,your answe is wrong") break elif a==3: print(b-c) answer=input('您的答案是') j=b-c if answer==j: print("right") else: print("sorry,your answe is wrong") break'''
修改完毕-正确代码
# -*- coding: utf-8 -*- import random def Math(): a=random.randint(1,3) b=random.randint(1,100) c=random.randint(1,100) if a == 1: # 求100以内两数相加的和 print ("%d + %d = ?" % (b, c)) answer = int(input('您的答案是: ')) j = b + c if answer == j: print("right!") else: print("sorry, your answer is wrong!") if a == 2: # 求100以内两数相乘的积 print ("%d * %d = ?" % (b, c)) answer = int(input('您的答案是: ')) j = b * c if answer == j: print("right!") else: print("sorry, your answer is wrong!") elif a == 3: # 求100以内两数相减的差 print ("%d - %d = ?" % (b, c)) answer = int(input('您的答案是: ')) j = b - c if answer == j: print("right!") else: print("sorry, your answer is wrong!") Math() while(True): panduan = input("欢迎来此训练,继续请按 1,退出请按 2: ") if int(panduan) == 1: Math() else: break
运行结果: