目录
程序简介
给定3个选项,每个选项3个随机整数,判断每一个选项能不能组成一个三角形。如果对了2个及以上,可以进入下一关;否则程序将会停止。
代码
from random import*
from time import*
T=15
chapter=1
while True:
print('')
print('')
print("************")
sleep(0.1)
print("T=",T)
sleep(0.1)
print("Chapter:",chapter)
sleep(0.1)
print("************")
sleep(0.1)
print('')
sleep(0.5)
a1=int(random()*T//1)+1
a2=int(random()*T//1)+1
a3=int(random()*T//1)+1
b1=int(random()*T//1)+1
b2=int(random()*T//1)+1
b3=int(random()*T//1)+1
c1=int(random()*T//1)+1
c2=int(random()*T//1)+1
c3=int(random()*T//1)+1
T*=2
sleep(0.5)
print("A: ",a1,a2,a3)
sleep(0.5)
print("B: ",b1,b2,b3)
sleep(0.5)
print("C: ",c1,c2,c3)
sleep(0.5)
print('')
print('"True"="1";"False"="0".')
print('')
sleep(0.5)
if a1+a2>a3 and a2+a3>a1 and a1+a3>a2:
Ka=1
else:
Ka=0
if b1+b2>b3 and b2+b3>b1 and b1+b3>b2:
Kb=1
else:
Kb=0
if c1+c2>c3 and c2+c3>c1 and c1+c3>c2:
Kc=1
else:
Kc=0
A=int(input("Please Enter The Value Of 'A':"))
sleep(0.5)
B=int(input("Please Enter The Value Of 'B':"))
sleep(0.5)
C=int(input("Please Enter The Value Of 'C':"))
print('')
score=0
AS=0
BS=0
CS=0
if A==Ka:
AS+=1
score+=1
if B==Kb:
BS+=1
score+=1
if C==Kc:
CS+=1
score+=1
if AS==1:
print("Choice 'A' Was True!")
sleep(0.1)
else:
print("Choice 'A' Was False!")
sleep(0.1)
if BS==1:
print("Choice 'B' Was True!")
sleep(0.1)
else:
print("Choice 'B' Was False!")
sleep(0.1)
if CS==1:
print("Choice 'C' Was True!")
sleep(0.1)
else:
print("Choine 'C' Was False!")
sleep(0.1)
sleep(0.25)
print('')
print("Total Score:",score)
sleep(0.1)
if score==3:
print("Perfect!")
sleep(0.1)
elif score==2:
print("Not Bad!")
sleep(0.1)
elif score==1:
print("Keep Trying!")
break
elif score==0:
print("Rubbish.")
break
chapter+=1
print("\nYou can go on.")
代码分析&注释
1、导入数据库
首先,导入random和time数据库。
from random import*
from time import*
(也可以 import random import time ,但是这样写需加上 random. 和 time. ,更麻烦,不推荐)
2、定义变量 T 和 chapter
定义变量 T(取数范围) 和 chapter(章节,关)。
T=15
chapter=1
3、while循环
while True 循环(当作答失败时打破)。
while True:
4、华丽输出提示
输出当前的 T 和 Chapter ,中间可等待,上下加上一排星号。
print('')
print('')
print("************")
sleep(0.1)
print("T=",T)
sleep(0.1)
print("Chapter:",chapter)
sleep(0.1)
print("************")
sleep(0.1)
print('')
sleep(0.5)
效果展示(当T=15,Chapter=1时):
************
T=15
Chapter=1
************
5、定义随机数变量
总共有A、B、C三个选项,每个选项3个随机数,所以定义 a1 a2 a3 b1 b2 b3 c1 c2 c3 。随机数的取值范围为:1~T 。在定义好后,可以将取值范围增加(在下一关会更难),T*=2就可以。
a1=int(random()*T//1)+1
a2=int(random()*T//1)+1
a3=int(random()*T//1)+1
b1=int(random()*T//1)+1
b2=int(random()*T//1)+1
b3=int(random()*T//1)+1
c1=int(random()*T//1)+1
c2=int(random()*T//1)+1
c3=int(random()*T//1)+1
T*=2
6、输出随机数变量
按顺序输出刚才定义的变量,并输出提示 "True"="1";"False"="0".
sleep(0.5)
print("A: ",a1,a2,a3)
sleep(0.5)
print("B: ",b1,b2,b3)
sleep(0.5)
print("C: ",c1,c2,c3)
sleep(0.5)
print('')
print('"True"="1";"False"="0".')
print('')
sleep(0.5)
效果展示(当T=15时):
A: 14 12 5
B: 4 4 12
C: 1 4 8
"True"="1";"False"="0".
7、判断是否可以组成三角
接下来要判断每个选项的3个数字能不能组成一个三角形(任意2边之和>第3边),直接用 if 就行。定义三个变量 Ka Kb Kc ,如果能赋值为1,不能为0。
if a1+a2>a3 and a2+a3>a1 and a1+a3>a2:
Ka=1
else:
Ka=0
if b1+b2>b3 and b2+b3>b1 and b1+b3>b2:
Kb=1
else:
Kb=0
if c1+c2>c3 and c2+c3>c1 and c1+c3>c2:
Kc=1
else:
Kc=0
8、输入答案
然后定义三个变量 A B C 为用户输入的答案,输入 A B C ,并附带提示语 " Please Enter The Value Of *** : " (译为 "请输入***的值:")
A=int(input("Please Enter The Value Of 'A':"))
sleep(0.5)
B=int(input("Please Enter The Value Of 'B':"))
sleep(0.5)
C=int(input("Please Enter The Value Of 'C':"))
print('')
9、判断对错
定义变量 score AS BS CS(分别代表总分,选项 A、B、C 的得分)。如果 A B C 中有哪一个选项答对了,则将相关联的变量( AS BS CS )赋值为 1 ,否则为 0 ; score的定义为答对题目的个数。
score=0
AS=0
BS=0
CS=0
if A==Ka:
AS+=1
score+=1
if B==Kb:
BS+=1
score+=1
if C==Kc:
CS+=1
score+=1
10、输出对错
如果选项 *** 对 / 错了,输出 "Choice *** Was True / False!"(译为 "选项 *** 是对/错的")。
if AS==1:
print("Choice 'A' Was True!")
sleep(0.1)
else:
print("Choice 'A' Was False!")
sleep(0.1)
if BS==1:
print("Choice 'B' Was True!")
sleep(0.1)
else:
print("Choice 'B' Was False!")
sleep(0.1)
if CS==1:
print("Choice 'C' Was True!")
sleep(0.1)
else:
print("Choine 'C' Was False!")
sleep(0.1)
sleep(0.25)
11、判断是否进入下一关
根据答对的数量,来输出总分及提示语句。最后如果进入了下一关,需将 chapter 加1,并输出提示语 "You can go on." 。
print('')
print("Total Score:",score)
sleep(0.1)
if score==3:
print("Perfect!")
sleep(0.1)
elif score==2:
print("Not Bad!")
sleep(0.1)
elif score==1:
print("Keep Trying!")
break
elif score==0:
print("Rubbish.")
break
chapter+=1
print("\nYou can go on.")
※ 所有变量:
T
组成三角形的数(a1,a2,a3,b1,b2,b3,c1,c2,c3)的取值范围。
chapter
此时的章节 / 关卡。
a1
A选项的第1个数。
a2
A选项的第2个数。
a3
A选项的第3个数。
b1
B选项的第1个数。
b2
B选项的第2个数。
b3
B选项的第3个数。
c1
C选项的第1个数。
c2
C选项的第2个数。
c3
C选项的第3个数。
Ka
A选项的标准答案。
Kb
B选项的标准答案。
Kc
C选项的标准答案。
A
用户输入的A选项的答案。
B
用户输入的A选项的答案。
C
用户输入的A选项的答案。
score
A,B,C总共答对的数量。
AS
A选项是否答对(对为1,错为0)。
BS
B选项是否答对(对为1,错为0)。
CS
C选项是否答对(对为1,错为0)。