Python1-100运算挑战教程

我是Python小小小小小小小小小小白,做得不好请见谅,或者你用Tkinter和Turtle实现也可以评论,这两个库我还不熟

首先导入基础模块

import random
import time

然后创建一些数字变量

print('1-100运算挑战!')
time.sleep(1)
print('请输入数字,输入其他的文本程序会出错并自动关闭')
time.sleep(1)
tk1 = str(random.randint(1,100))
tk2 = str(random.randint(1,100))
kta = int(random.randint(1,100))
ktb = str(kta - random.randint(1,30))
bk1 = str(random.randint(1,100))
bk2 = str(random.randint(1,10))
kba = int(random.randint(1,100))
kbb = int(random.randint(1,100))

我们还得用while循环保证被除数除以除数不会出现小数点
否则商可能是无限不循环或无限循环小数

用%实现

if kba % kbb !=0:
	while kba % kbb !=0:
		kbb = int(random.randint(1,100))

用被除数除以除数,取余数,如果余数不等于0,那么重新赋值除数,就这样一直循环
接着是算法

加法

# 使算法循环执行
while True:
	# 将得数整体转化为字符串
	tk = str(input(tk1+'+'+tk2+'='))
	# 如果得数等于加数1+加数2,那么说‘答对了’
	if int(tk) ==int(float(tk1)+float(tk2)):
		print('答对了')
		# 得数不等于加数1+加数2则说‘答错了’
	else:
		print('答错了')
	# 重置加数1与加数2
	tk1 = str(random.randint(1,100))
	tk2 = str(random.randint(1,100))
	# 等待0.5秒
	time.sleep(0.5)

减法

kt = str(input(str(kta)+'-'+ktb+'='))
if int(kt) ==int(float(kta)-float(ktb)):
	print('答对了')
else:
	print('答错了')
kta = int(random.randint(1,100))
ktb = str(kta - random.randint(1,30))
time.sleep(0.5)

将减法算法加入到加法算法的下面

while True:
	tk = str(input(tk1+'+'+tk2+'='))
	if int(tk) ==int(float(tk1)+float(tk2)):
		print('答对了')
	else:
		print('答错了')
	tk1 = str(random.randint(1,100))
	tk2 = str(random.randint(1,100))
	time.sleep(0.5)
	kt = str(input(str(kta)+'-'+ktb+'='))
	if int(kt) ==int(float(kta)-float(ktb)):
		print('答对了')
	else:
		print('答错了')
	kta = int(random.randint(1,100))
	ktb = str(kta - random.randint(1,30))
	time.sleep(0.5)

乘法与加法差不多,只需要改一些参数,你们可以自己试试

分割

















分割

实在想不出来也可以看看我的

乘法与除法

bk = str(input(bk1+'x'+bk2+'='))
if int(bk) ==int(float(bk1)*float(bk2)):
	print('答对了')
else:
	print('答错了')
bk1 = str(random.randint(1,100))
bk2 = str(random.randint(1,10))
time.sleep(0.5)
kb = str(input(str(kba)+'/'+str(kbb)+'='))
if int(kb) ==int(float(kba)/float(kbb)):
	print('答对了')
else:
	print('答错了')
kba = int(random.randint(1,100))
kbb = int(random.randint(2,100))
if kba % kbb !=0:
	while kba % kbb !=0:
		kbb = int(kba - random.randint(2,100))
time.sleep(0.5)

接下来我们只需要将乘法与除法连接到加法与减法的下面

连接

while True:
	tk = str(input(tk1+'+'+tk2+'='))
	if int(tk) ==int(float(tk1)+float(tk2)):
		print('答对了')
	else:
		print('答错了')
	tk1 = str(random.randint(1,100))
	tk2 = str(random.randint(1,100))
	time.sleep(0.5)
	kt = str(input(str(kta)+'-'+ktb+'='))
	if int(kt) ==int(float(kta)-float(ktb)):
		print('答对了')
	else:
		print('答错了')
	kta = int(random.randint(1,100))
	ktb = str(kta - random.randint(1,30))
	time.sleep(0.5)
	bk = str(input(bk1+'x'+bk2+'='))
	if int(bk) ==int(float(bk1)*float(bk2)):
		print('答对了')
	else:
		print('答错了')
	bk1 = str(random.randint(1,100))
	bk2 = str(random.randint(1,10))
	time.sleep(0.5)
	kb = str(input(str(kba)+'/'+str(kbb)+'='))
	if int(kb) ==int(float(kba)/float(kbb)):
		print('答对了')
	else:
		print('答错了')
	kba = int(random.randint(1,100))
	kbb = int(random.randint(2,100))
	if kba % kbb !=0:
		while kba % kbb !=0:
			kbb = int(kba - random.randint(2,100))
	time.sleep(0.5)

算法部分就做好了,接下来是完整代码

完整代码

import random
import time
print('1-100运算挑战!')
time.sleep(1)
print('请输入数字,输入其他的文本程序会出错并自动关闭')
time.sleep(1)
tk1 = str(random.randint(1,100))
tk2 = str(random.randint(1,100))
kta = int(random.randint(1,100))
ktb = str(kta - random.randint(1,30))
bk1 = str(random.randint(1,100))
bk2 = str(random.randint(1,10))
kba = int(random.randint(1,100))
kbb = int(random.randint(1,100))
if kba % kbb !=0:
	while kba % kbb !=0:
		kbb = int(random.randint(1,100))
while True:
	tk = str(input(tk1+'+'+tk2+'='))
	if int(tk) ==int(float(tk1)+float(tk2)):
		print('答对了')
	else:
		print('答错了')
	tk1 = str(random.randint(1,100))
	tk2 = str(random.randint(1,100))
	time.sleep(0.5)
	kt = str(input(str(kta)+'-'+ktb+'='))
	if int(kt) ==int(float(kta)-float(ktb)):
		print('答对了')
	else:
		print('答错了')
	kta = int(random.randint(1,100))
	ktb = str(kta - random.randint(1,30))
	time.sleep(0.5)
	bk = str(input(bk1+'x'+bk2+'='))
	if int(bk) ==int(float(bk1)*float(bk2)):
		print('答对了')
	else:
		print('答错了')
	bk1 = str(random.randint(1,100))
	bk2 = str(random.randint(1,10))
	time.sleep(0.5)
	kb = str(input(str(kba)+'/'+str(kbb)+'='))
	if int(kb) ==int(float(kba)/float(kbb)):
		print('答对了')
	else:
		print('答错了')
	kba = int(random.randint(1,100))
	kbb = int(random.randint(2,100))
	if kba % kbb !=0:
		while kba % kbb !=0:
			kbb = int(kba - random.randint(2,100))
	time.sleep(0.5)

最后的最后你们可以自己加上分数、限制时间等,我就不说了

你可能感兴趣的:(python)