1.打印
"""
*
***
*****
*******
*********
***********
"""
n = int(input("请输入行数:"))
for i in range(1,n+1):
print("*"*(2*i-1),end=" ")
print()
2.
"""
*
**
***
****
*****
******
"""
n = int(input("请输入行数:"))
for i in range(1,n+1):
for j in range(1,n-i+1):
print(" ",end=" ")
for j in range(1,i+1):
print("*",end=" ")
print()
3.
"""
*
***
*****
*******
*********
***********
"""
n = int(input("请输入行数:"))
for i in range(1,n+1):
for j in range(1,n-i+1):
print(" ",end=" ")
for j in range(1,2*i):
print("*",end=" ")
print()
4.
"""
***********
*********
*******
*****
***
*
"""
n = int(input("请输入行数:"))
for i in range(1,n+1):
for j in range(1,i):
print(" ",end=" ")
for j in range(1,2*n-2*i+2):
print("*",end=" ")
print()
5.
"""
*
***
*****
*******
*********
***********
*********
*******
*****
***
*
"""
n = int(input("请输入行数:"))
a = n//2
b = n-a
for i in range(1,a+1):
for j in range(1,a-i+2):
print(" ",end=" ")
for j in range(1,2*i):
print("*",end=" ")
print()
for i in range(1,b+1):
for j in range(1,i):
print(" ",end=" ")
for j in range(1,2*b-2*i+2):
print("*",end=" ")
print()
6.
"""
*
* *
* *
* *
* *
* *
* *
* *
* *
* *
*
"""
n = 6
for i in range(n):
for j in range(n-i-1):
print(" ", end="")
for j in range(2*i+1):
if j == 0 or j == 2*i:
print("*", end="")
else:
print(" ", end="")
print()
for i in range(n-2, -1, -1):
for j in range(n-i-1):
print(" ", end="")
for j in range(2*i+1):
if j == 0 or j == 2*i:
print("*", end="")
else:
print(" ", end="")
print()
7.
n = int(input('请输入菱形每边星星个数,n= '))
m = t = 2 * n-1
while m >= 1:
if(m == t or m == 1):
print(' ' * n + '*' + ' ' * 4 * (n-1))
elif m == n:
print(' '+' *' * (2 * n - 2))
elif m > n:
print(' ' * (m - n + 1) + '*' +' ' * (t - m - 1) + ' *' +' ' * (t - m - 1) + ' *')
else:
print(' ' * (n - m + 1) + '*' + ' ' * ((2 * m - 3) // 2) + ' *' + ' ' * ((2 * m - 3) // 2) + ' *')
m -= 1
8.用户输入两个数a、b。如果a能被b整除或a加b大于1000,则输出a;否则输出b
a = int(input("请输入第一个数:"))
b = int(input("请输入第二个数:"))
if a // b or (a + b) > 1000:
print(a)
else:
print(b)
9.请输入一个数,判断这个数是偶数还是奇数,如果使用偶数,请判断从1到该数是3的倍数有哪些,如果是奇数,请判断从1到该数是5的倍数有哪些
a = int(input("请输入一个数:"))
if a & 1 == 0:
for i in range(1,a):
if i % 3 == 0:
print(i)
else:
for i in range(1,a):
if i % 5 == 0:
print("i")
10.某商店T恤的价格为35元/件(2件9折,3件以上8折),裤子的价格为120 元/条(2条以上9折).小明在该店买了3件T恤和2条裤子,请计算并显示小明应该付多少钱?
a = int(input("请输入购买T恤数:"))
b = int(input("请输入购买裤子数:"))
m = 0
n = 0
if a == 2:
m = 35*a*0.9
elif a >= 3:
m = 35*a*0.8
if b >= 2:
n = 120*b*0.9
print("小明应该付:%s"%(m+n))
11.鸡兔同笼,从上面看有35个头,从下面看有94只脚,请问鸡有几只,兔有几只?
h = int(input("输入头的数量:"))
f = int(input("输入脚的数量:"))
a = 0
b = 0
if f & 1 != 0:
print("输入数据有误!!!")
else:
a = (f - h*2)/2
b = h - a
print("兔子有%d只,鸡有%d只"%(a,b))
12.猜拳游戏:石头、剪刀、布的游戏
import random
computer = random.randint(1,3)
user = int(input("请出拳:1(石头),2(剪刀),3(布)"))
while 1 <= user <= 3:
if user == computer:
print("平局")
break
elif (user == 1 and computer == 3) or (user == 3 and computer == 2) or (user == 2 and computer == 1):
print("电脑胜利")
break
elif (user == 1 and computer == 2) or (user == 2 and computer == 3) or (user == 3 and computer == 1):
print("玩家胜利")
break
13.判断以下哪些不能作为标识符 B,D,F
A、a\n
B、¥a\n
C、_12\n
D、$a@12\n
E、false\n
F、False
不能以数字开头
不建议使用内置对象作为标识符
不能用关键字作标识符
14.求50~150之间的质数是那些?
for i in range(50,151):
k = 0
for j in range(1,i + 1):
if i % j == 0:
k += 1
if k == 2:
print(i)#14,求50~150之间的质数是那些?
for i in range(50,151):
k = 0
for j in range(1,i + 1):
if i % j == 0:
k += 1
if k == 2:
print(i)
15.打印输出标准水仙花数,输出这些水仙花数
for i in range(100,1000):
a = i//100
b = i//10%10
c = i%100%10
if i == a**3+b**3+c**3:
print(i)
16.验证:任意一个大于9的整数减去它的各位数字之和所得的差,一定能被9整除.
num = int(input("请任意输入一个数:"))
a = 0
num = 0
while num != 0:
a += num % 10
num //= 10
if (num - a) % 9 == 0:
print('符合')
else:
print("不符合")
17.一个五位数,若在它的后面写上一个7,得到一个六位数A,若在它前面写上一个7,得到一个六位数B,B是A的五倍,求此五位数.
for i in range(10000,100000):
a = i*10 + 7
b = i + 7*100000
if b / a == 5:
print(i)