第一题
#直角三角形
'''a = int (input())
i = 1
while i<=a:
j = 1
while j <= i:
print("*",end = "")
j+=1
print ()
i +=1
'''
#胖直角三角形
'''
a = int (input ("请输入你要打印的层数"))
for i in range (1,a+1):
for j in range (1,2*i):
print ("*",end = "")
print ()
'''
#镜面直角三角形
'''
a = int (input ("请输入你要打印的层数"))
for i in range (1,a+1):
for j in range (a-i):
print (" ", end = "")
for s in range (i):
print ("*", end = "")
print ()
'''
#等腰三角形
'''
a = int (input ("请输入你要打印的层数"))
for i in range (1,a+1):
for s in range(a-i):
print (" ", end = "")
for k in range (2*i-1):
print ("*", end = "")
print ()
'''
#倒等腰三角形
'''
a = int (input ("请输入你要打印的层数"))
for i in range (a,0,-1):
for s in range(a-i):
print (" ", end = "")
for k in range (2*i-1):
print ("*", end = "")
print ()
'''
#菱形
'''
a = int (input ("请输入你要打印的层数"))
while a%2==0:
a= int (input ("对不起 请重新输入"))
for i in range (1,a//2+2):
for s in range(a-i):
print (" ", end = "")
for k in range (2*i-1):
print ("*", end = "")
print ()
for i in range (a//2,0,-1):
for s in range(a-i):
print (" ", end = "")
for k in range (2*i-1):
print ("*", end = "")
print ()
'''
#空心菱形
'''
a = int (input())
while a%2==0:
a = int (input("对不起 请重新输入"))
for i in range (1,a//2+2):
for j in range (a-i):
print (" ",end ="")
for k in range(2*i-1):
if k==0 or k==2*i-2:
print ("*",end = "")
else :
print (" ",end = "")
print ()
'''
第二题
p = int(input())
f = True
for i in range(2, prime // 2 + 1):
if p % 2 == 0:
f = False
break
if f:
print("是质数")
else:
print("不是")
第三题
不是很会,写的比较丑陋,希望老师课上讲解
第四题
s = 0
for num in range (306,10000,60):
if num%2==0 and num%3==0:
s = num
break
r = 0
for num in range (963,0,-60):
if num%2==0 and num%3==0:
r = num
break
print ("满足条件最小的四位数",s)
print ("满足条件最大的四位数",r)
第五题
for i in range(1000,10000):
gewei = (i%1000)%100%10
shiwei = (i%1000)%100//10
baiwei = (i%1000)//100
qianwei = i//1000
change = gewei*1000+shiwei*100+baiwei*10+qianwei
if (i*qianwei) == change:
print(i,end=" ")
运行结果PS D:\桌面\3.9python周末班\课堂代码> & D:/python/python.exe d:/桌面/3.9python周末班/课堂代码/作业2/第五题.py
1001 1111 1221 1331 1441 1551 1661 1771 1881 1991
第六题
a = int (input ("请输入一个数字"))
b = int (input ("请输入第二个数字"))
if a + b > 1000 or b//a==0:
print (a)
else:
print (b)
运行结果 :PS D:\桌面\3.9python周末班\课堂代码> & D:/python/python.exe d:/桌面/3.9python周末班/课堂代码/作业2/第六题.py
请输入一个数字8
请输入第二个数字2
8
第七题
a = int (input ("请输入一个数字"))
if a%2==0:
print ("a是偶数")
for i in range (1,a+1):
if i % 3 ==0:
print (i)
else:
print ("a是奇数")
for i in range (1,a+1):
if i % 5 ==0:
print (i)
运行结果:
PS D:\桌面\3.9python周末班\课堂代码> & D:/python/python.exe d:/桌面/3.9python周末班/课堂代码/作业2/第七题.py
请输入一个数字8
a是偶数
3
6
PS D:\桌面\3.9python周末班\课堂代码> & D:/python/python.exe d:/桌面/3.9python周末班/课堂代码/作业2/第七题.py
请输入一个数字11
a是奇数
5
10
第八题
a = ("某商店T恤的价格为35元/件(2件9折,3件以上8折),裤子的价格为120 元/条(2条以上9折)")
print (a)
print (f'T-shit= ({35}*{2})*{0.9}+{35}={((35*2)*0.9)+35}')
print (f'trousers = {120}+{120}={120+120}')
运行结果:
PS D:\桌面\3.9python周末班\课堂代码> & D:/python/python.exe d:/桌面/3.9python周末班/课堂代码/作业2/第八题.py
某商店T恤的价格为35元/件(2件9折,3件以上8折),裤子的价格为120 元/条(2条以上9折)
T-shit= (35*2)*0.9+35=98.0
trousers = 120+120=240
第九题
for ji in range(0 , 36):
tuzi = 35 - ji
if 2 * ji + 4 * tuzi == 94:
print(f"鸡的数量为:{ji}, 兔的数量为:{tuzi}")
运行结果:
PS D:\桌面\3.9python周末班\课堂代码> & D:/python/python.exe d:/桌面/3.9python周末班/课堂代码/作业2/鸡兔同笼.py
鸡的数量为:23, 兔的数量为:12
第十题
import random as r
compute = r.randint(0, 100)
while True:
i = int(input("请输入一个0~100的整数:"))
if i > compute:
print("不好意思,猜大了")
elif i < compute:
print("不好意思,猜小了")
else:
print("恭喜您,猜对了")
choice = input("请是否继续猜(Y/y),其他任意符号退出:")
if choice == "Y" or choice == "y":
compute = r.randint(0, 100)
else:
print("拜拜")
break
第十一题
import random
person = int(input('请输入:石头(0)、剪刀(1)、布(2):'))
computer = random.randint(0, 2)
if person == 0:
print('玩家:石头')
elif person == 1:
print('玩家:剪刀')
else:
print('玩家:布')
if computer == 0:
print('电脑:石头')
elif computer == 1:
print('电脑:剪刀')
else:
print('电脑:布')
if person == computer:
print('你好厉害呀!居然和我打成平局!')
elif person == 0 and computer == 1 or person == 1 and computer == 2 or person == 2 and computer == 0:
print('恭喜你,你赢了!')
else:
print('真遗憾,你输了!')