1.打印三角形相关图形
01
*
***
*****
*******
*********
***********
layer = int(input("请输入您打印的行:"))
for i in range(1, layer + 1):
print("*" * i)
02.
*
**
***
****
*****
******
layer = int(input("请输入您打印的行:"))
for i in range(1, layer + 1):
print("*" * (2 * i - 1))
03.
***********
*********
*******
*****
***
*
layer = int(input("请输入您打印的行:"))
for i in range(1, layer + 1):
# 打印空格数量
space_num = layer - i
for j in range(space_num):
print(" ", end="")
star_num = i
for j in range(star_num):
print("*", end="")
print()
for i in range(1, layer + 1):
print(" " * (layer - i), end="")
print("*" * i)
04,菱形
layer = int(input("请输入您打印的行:"))
for i in range(1, (layer // 2 + 2)):
for j in range(layer - i):
print(" ", end="")
for j in range(2 * i - 1):
print("*", end="")
print()
for i in range(layer // 2, 0, -1):
for j in range(layer - i):
print(" ", end="")
for j in range(2 * i - 1):
print("*", end="")
print()
05.空心菱形
layer = int(input("请输入您打印的行:"))
for i in range(1, (layer // 2 + 2)):
for j in range(layer - i):
print(" ", end="")
for j in range(2 * i - 1):
if j == 0 or j == 2 * i - 2:
print("*", end="")
else:
print(" ", end="")
print()
for i in range(layer // 2, 0, -1):
for j in range(layer - i):
print(" ", end="")
for j in range(2 * i - 1):
if j == 0 or j == 2 * i - 2:
print("*", end="")
else:
print(" ", end="")
print()
06.空心菱形
layer = int(input("请输入您打印的行:"))
for i in range(1, (layer // 2 + 2)):
for j in range(layer - i):
print(" ", end="")
for j in range(2 * i - 1):
if j == 0 or j == 2 * i - 2 or j == i - 1 or i == layer // 2 + 1:
print("*", end="")
else:
print(" ", end="")
print()
for i in range(layer // 2, 0, -1):
for j in range(layer - i):
print(" ", end="")
for j in range(2 * i - 1):
if j == 0 or j == 2 * i - 2 or j == i - 1:
print("*", end="")
else:
print(" ", end="")
print()
2.用户输入两个数a、b。如果a能被b整除或a加b大于1000,则输出a;否则输出b。
a=int(input("请输入第一个数"))
b=int(input("请输入第一个数"))
if a//b==0 or a+b==1000:
print(a)
else:
print(b)
3.请输入一个数,判断这个数是偶数还是奇数,如果是偶数,请判断从1到该数是3的倍数有哪些,如果是奇数,请判断从1到该数是5的倍数有哪些
num = int(input('请输入一个数:'))
if num % 2 == 0:
print(f'{num}为偶数,从1到{num}是3的倍数有:')
for x in range(1, num + 1):
if x % 3 == 0:
print(x)
else:
print(f'{num}为奇数,从1到{num}是5的倍数有:')
for x in range(1, num + 1):
if x % 5 == 0:
print(x)
4.某商店T恤的价格为35元/件(2件9折,3件以上8折),裤子的价格为120 元/条(2条以上9折).小明在该店买了3件T恤和2条裤子,请计算并显示小明应该付多少钱?
shirt = 35
jiage = 120
a = int(input('请输入买T恤的件数:'))
b = int(input('请输入买裤子的件数:'))
if a == 2:
shirt = shirt * 0.9
if a > 3:
shirt = shirt * 0.8
if b > 2:
jiage = jiage * 0.9
print(f'小明应付{a * shirt + b * jiage}元')
5.鸡兔同笼,从上面看有35个头,从下面看有94只脚,请问鸡有几只,兔有几只?
for x in range(1, 35):
y = 35 - x
if 4 * x + 2 * y == 94:
print('兔子有%s只,鸡有%s只' % (x, y))
for x in range(1, 23):
y = 35 - x
if 4 * x + 2 * y == 94:
print('兔子有%s只,鸡有%s只' % (x, y))
6.猜拳游戏:石头、剪刀、布的游戏x = int(input("请输入:剪刀(0)、石头(1)、布(2):"))
x = int(input("请输入:剪刀(0)、石头(1)、布(2):"))
if x != 1 and x != 2 and x!= 0:
print("请输入正确的数字")
exit()
import random
suiji = random.randint(0,2)
print("随机生成数字为:",suiji)
if x == 0:
print("你的输入为:剪刀(0)")
if suiji == 0:
print("平局,再来一局")
elif suiji == 1:
print("哈哈,你输了")
else:
print("恭喜你,你赢了")
elif x == 1:
print("你的输入为:石头(1)")
if suiji == 0:
print("恭喜你,你赢了")
elif suiji == 1:
print("平局,再来一局")
else:
print("哈哈,你输了")
elif x == 2:
print("你的输入为:布(2)")
if suiji == 0:
print("哈哈,你输了")
elif suiji == 1:
print("恭喜你,你赢了")
else:
print("平局,再来一局")
7.判断以下哪些不能作为标识符 BDF
A、a
B、¥a
C、_12
D、$a@12BDEF
E、false
F、False
8.求50~150之间的质数是那些?
for num in range(50, 151):
flag = True
for i in range(2, num):
if num % i == 0:
flag = False
break
if flag:
print(num)
9.打印输出标准水仙花数,输出这些水仙花数
for i in range(100, 1000):
a = i // 100
b = i % 100 // 10
c = i % 10
if a ** 3 + b * b * b + c ** 3 == i:
print(i)
10.验证:任意一个大于9的整数减去它的各位数字之和所得的差,一定能被9整除.
count = 0
src_num = num
while num > 0:
count += num % 10
num //= 10
if (src_num - count) % 9 == 0:
print("验证通过")
11.一个五位数,若在它的后面写上一个7,得到一个六位数A,
若在它前面写上一个7,得到一个六位数B,B是A的五倍,求此
五位数.
for i in range(10000, 100000):
a = i // 10000
b = i % 10000 // 1000
c = i % 1000 // 100
d = i % 100 // 10
e = i % 10
A = int(str(a) + str(b) + str(c) + str(d) + str(e) + "7")
B = int("7" + str(a) + str(b) + str(c) + str(d) + str(e))
if B // 5 == A:
print(i)
12、小明今年18岁了,正常来说,应该在蛋糕上,插入18跟蜡烛,但是小明不是一般人,他是程序员,他考虑到18的进制是10010,所以他在蛋糕上插入了五个蜡烛,点燃了其中两跟,问:如果用户输入年龄,请你安排插入的蜡烛数和点燃的蜡烛数。
例如输入18
输出5 2
num = int(input("请输入一个数:"))
temp = []
while num > 0 :
temp.append(num % 2)
num //= 2
num1 = 0
num2= 0
#判断两种蜡烛的数量
for i in temp :
if i == 1 :
num1+=1
else :
num2+=1
print(f"蜡烛一共{num1 + num2}根,点燃的一共{num1}根")