1.使用循环输出九九乘法表。
for i in range(1, 10):
for m in range(1, i+1):
print(m, "X", i, '=', i * m, end=' ')
if i == m :
print('\n')
for i in range(1,10):
for j in range(1,i+1):
print("%2d * %2d = %2d" % (j, i, i * j), end = " ")
print()
-
使用循环输出等腰三角形
等腰三角形.png
m = 1
for i in range(4):
print((4-i)*" ",m*"*")
m = m+2
lines = int(input("输入要打印的行数:"))
for i in range(lines):
for j in range(0, lines - i):
print(end=" ")
for k in range(2 * i + 1):
print("*", end="")
print()
- 给定奇数3 , 输出(横、坚、斜的总和相等) :
08 01 06
03 05 07
04 09 02
给定奇数5 , 输出(横、坚、斜的总和相等) :
17 24 01 08 15
23 05 07 14 16
04 06 13 20 22
10 12 19 21 03
11 18 25 02 09
依此类推。
n = int(input("请输入一个奇数:"))
m = [[0 for i in range(n)] for i in range(n)] #矩阵
row, col = 0, n // 2
num = 1
while num <= n * n:
m[row][col] = num
num += 1
row -= 1
col += 1
if row < 0 and col >= n:
row, col = row + 2, col - 1
#因为row已经减了1,col已经加了1
elif row < 0:
row = n - 1
elif col >= n:
col = 0
elif m[row][col] != 0:
row, col = row + 2, col - 1
for i in range(n):
for j in range(n):
print('%02d' % m[i][j], end = ' ')
print()
首居上行正中央,依次斜向右上方,上出框时往下写,右出框时左边放,排重退回下格填,右上排重一个样。
填写规律
如果右上角的位置的行和列都越界,就将这个数放在上个数的正下方
如果右上角的位置只有行越界,就将行改到最大,列不变
如果右上角的位置只有列越界,就将列改到最小,行不变
如果右上角的位置没有越界,但是已经填有数字,就将这个数放在上个数的正下方
size = int(input("输入要打印的SIZE(奇数):"))
array = [[0] * size]
# 创建一个长度size * size的二维列表
for i in range(size - 1):
array += [[0] * size]
row, col = 0, size // 2
for i in range(1, size * size + 1):
array[row][col] = i
if i % size == 0:
row += 1
elif row == 0:
row = size - 1
col += 1
elif col == size - 1:
row -= 1
col = 0
else:
row -= 1
col += 1
for i in range(size):
for j in range(size):
print('%02d' % array[i][j], end=" ")
print()
- 使用循环输出菱形。用户输入偶数,则提示不能打印
lines = int(input("输入要打印的行数(奇数):"))
if lines % 2 == 0:
print('不能打印')
import sys
sys.exit(0)
half_lines = lines // 2 + 1
# 打印上半
for i in range(half_lines):
print(" " * (half_lines - i), end="")
print("*" * (2 * i + 1))
# 打印下半
for i in range(half_lines - 1):
print(" " * (i + 2), end="")
print("*" * (lines - 2 - 2 * i))
5.使用循环输出空心菱形。用户输入偶数, 则提示不能打印
空心菱形.png
lines = int(input("输入要打印的行数(奇数):"))
if lines % 2 == 0:
print('不能打印')
import sys
sys.exit(0)
half_lines = lines // 2 + 1
# 打印上半
for i in range(half_lines):
print(" " * (half_lines - i), end="")
if i == 0:
print("*")
else:
print("*", end="")
print(" " * (2 * i - 1), end="")
print("*")
# 打印下半
for i in range(half_lines - 1):
print(" " * (i + 2), end="")
if i == half_lines - 2:
print("*")
else:
print("*", end="")
print(" " * (lines - 4 - 2 * i), end="")
print("*")
- 用户输入自己的成绩,程序会自动判断该成绩的类型: 成绩≥90 分用A表示, 8 0~89 分用B表示, 7 0~7 9 分用C表示,其他的用D表示
score = int(input("输入你的成绩:"))
if score >= 90:
print('您的成绩为A')
elif score < 90 and score >= 80:
print('您的成绩为B')
elif score < 80 and score >= 70:
print('您的成绩为C')
else:
print('您的成绩为D')
while(True):
s = input("输入自己的成绩:")
if s == 'exit':
import sys
sys.exit(0)
score = float(s)
if score >= 90:
print('A')
elif 90 > score >= 80:
print('B')
elif 80 > score >= 70:
print('C')
else:
print('D')
7.判断101~200之间有多少个素数,并输出所有的质数
list = [i for i in range (101, 201)]
c = 0
for i in list:
for j in range(2, i):
if i%j== 0:
break
else:
c+=1
print(i)
print('质数共有:',c)
start = 101
end = 200
for i in range(101, end + 1):
is_prime = True
for j in range(2 , int(i ** 0.5) + 1):
if i % j == 0:
is_prime = False
if is_prime:
print(i)
8 . 打印出所有的“水仙花数”。所谓“水仙花数”,是指一个三位数,其各位数字的立方和等于该数本身。例如, 15 3 是一个“水仙花数”,因为153 = 13 +53+33
list = [i for i in range (101, 1000)]
for i in list:
num1 = i//100
num2 = i//10%10
num3 = i%10
value = num1**3+num2**3+num3**3
if value == i:
print(value,end=" ")
# 方法一
start = 101
end = 999
for i in range(start, end + 1):
# 计算百位上的数
bai = i // 100
# 计算十位、个位上的数
shi, ge = (i - bai * 100) // 10, i % 10
# 判断是否为水仙花数
if ge ** len(str(i)) + shi ** len(str(i)) + bai ** len(str(i)) == i:
print(i)
# 方法二
start = 1
end = 10 ** 7
for i in range(start, end + 1):
sm = 0
for j in str(i):
sm += (ord(j) - 48) ** len(str(i))
# 判断是否为水仙花数
if sm == i:
print(i)
# 方法三
a =[j for j in range(1, 10 ** 5)
if sum([(ord(i)-48) ** len(str(j)) for i in str(j)]) == j]
print(a)
- 输入一行字符, 分别统计出其中英文字母、空格、数字和其他字符的个数。
s = input("请输入:")
blank_count = 0
letter_count = 0
number_count = 0
other_count = 0
for i in range(len(s)):
if s[i].encode('UTF-8').isalpha():
letter_count += 1
elif s[i].isdigit():
number_count += 1
elif s[i].isspace():
blank_count += 1
else:
other_count += 1
print(f'输入字符中共有字母{letter_count}个,数字{number_count}个,空格{blank_count}个,其他字符{other_count}个')
string = input("请输入一行内容: ")
char_num, digit_num, other_num = 0, 0, 0
for c in string:
if c.isdigit(): digit_num += 1
elif c.isalpha(): char_num += 1
else: other_num += 1
print('字母个数', char_num)
print('数字个数', digit_num)
print('其他字母个数', other_num)
-
打印出如下所示的近似圆,只要给定不同的半径,圆的大小就会随之发生改变(如果需要使用复杂的数学运算 ,则可使 Python math 模块)
圆.png
radius = int(input("请输入半径: "))
for i in range(2 * radius + 1):
half = round((radius ** 2 - (radius - i) ** 2) ** 0.5)
print(" " * (radius - half), end="")
print("*", end="")
print(" " * half * 2, end="")
print("*")
- 给定3 , 输出:
----c----
--c-b-c--
c-b-a-b-c
--c-b-c--
----c----
给定4,输出:
------d------
----d-c-d----
--d-c-b-c-d--
d-c-b-a-b-c-d
--d-c-b-c-d--
----d-c-d----
------d------
给定5,输出:
--------e--------
------e-d-e------
----e-d-c-d-e----
--e-d-c-b-c-d-e--
e-d-c-b-a-b-c-d-e
--e-d-c-b-c-d-e--
----e-d-c-d-e----
------e-d-e------
--------e--------
以此类推。
- 完善数字转人民币读法的程序
网友评论