1.打印各种图形
A.直角三角形
a = int(input("请输入行数: "))
i = 0
while i < a:
print("*"*10)
i+=1
B.直角三角形
a = int(input("请输入行数: "))
i = 0
while i<a:
print("*"*(i+1))
i+=1
C.反直角三角形
i = 1
n = int(input("请输入行数:"))
while i <= n:
j = 1
while j <= n-i:
print(' ',end=' ')
j += 1
k = 1
while k <= i:
print('*',end=' ')
k += 1
print()
i += 1
D.菱形
第一种:
n = int(input("请输入菱形的边长:"))
for i in range(n):
print(" " * (n - i - 1) + "*" * (2 * i + 1))
for i in range(n - 2, -1, -1):
print(" " * (n - i - 1) + "*" * (2 * i + 1))
第二种:
n = int(input("请输入菱形的边长:"))
while n % 2 == 0:
n = int(input("请输入奇数行:"))
for i in range(1,(n // 2 + 2)): #打印上半截,比一半还多一行,所以加一;还加了一个1,是因为for循环中,后面那一位加不到
for j in range(n - 1):
print(" ",end="")
for j in range(2 * n - 1):
print("*",end="")
print()
for i in range(n // 2,0,-1):
for j in range(n - i):
print(" ",end="")
for j in range(2 * i -1):
print("*",end="")
print()
E.空心菱形
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()
F.对角线菱形
layer = int(input("请输入你想要的行数:"))
while layer % 2 == 0:
layer = int(input("请输入奇数行:"))
for i in range(layer // 2 + 2):
for j in range(layer - i):
print(" ",end = "")
for j in range(2 * i - 1):
# j==0和j==2*i-1和j==(2*i-2)/2和最后一行都要打出*,注意:j控制列,i控制行!
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.判断奇数和偶数
um = int(input("请输入一个整数:"))
if num & 1 == 0:
print(f"{num}为even_number,从1到{num}是3的倍数有:")
for i in range(1, num + 1):
if i % 3 == 0:
print(i)
else:
print(f"{num}是odd_number,从1到{num}是5的倍数有:")
for x in range(1,num + 1):
if x % 5 == 0:
print(x)
4.应用题
A.某商店T恤的价格为35元/件(2件9折,3件以上8折),裤子的价格为120 元/条(2条以上9折).小明在该店买了3件T恤和2条裤子,请计算并显示小明应该付多少钱?
shirt = 35
trousers = 120
x = int(input('请输入买T恤的件数:'))
y = int(input('请输入买裤子的件数:'))
if x == 2:
shirt = shirt * 0.9
if x > 3:
shirt = shirt * 0.8
if y > 2:
trousers = trousers * 0.9
print(f'小明应付{x * shirt + y * trousers}元')
B.鸡兔同笼,从上面看有35个头,从下面看有94只脚,请问鸡有几只,兔有几只?
第一种:
for x in range(1, 36):
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))
5.猜拳游戏
import random
computer = random.randint(0,2)
user = int(input("请出拳:0(石头),1(剪刀),2(布)"))
while 0 <= user <= 2:
if user == computer:
print("平局")
break
elif (user == 0 and computer == 2) or (user == 0 and computer == 1) or (user == 1 and computer == 0):
print("电脑胜利")
break
elif (user == 0 and computer == 1) or (user == 1 and computer == 2) or (user == 2 and computer == 0):
print("玩家胜利")
break
6.判断题
答案:BDF
7. 求50~150之间的质数是那些?
num = int(input("请输入一个数:"))
flag = 1 #j假设每一个传入的数据都是质数
for i in range(2,num // 2 + 1): #对代码进行优化;只需要进行该数的一半;
if num % i == 0:
flag = 0
break #跳出循环;因为当第六行代码执行的时候,这个数就百分百不是质数了;
if flag:
print(f"{num}是质数!")
else:
print(f"{num}不是质数!")
8. 打印输出标准水仙花数,输出这些水仙花数
for i in range(100,1000):
a = i // 100
b = i % 100 // 10
c = i % 10
if a ** 3 + b ** 3 + c ** 3 == i:
print(i)
9.验证:任意一个大于9的整数减去它的各位数字之和所得的差,一定能被9整除
num = int(input("请输入一个大于9的整数:"))
count = 0
src_num = num
while num > 0:
count += num % 10 #把个位数弄出来
num //= 10 #将个位数移出去
if (src_num - count) % 9 == 0:
print(f"{num}验证成功")
else:
print(f"{num}验证失败")
10.一个五位数,若在它的后面写上一个7,得到一个六位数A,若在它前面写上一个7,得到一个六位数B,B是A的五倍,求此五位数
for i in range(10000,100000):
A = i * 10 + 7
B = 7 * 100000 + i
if B // A == 5:
print(i)