- #####求s=a+aa+aaa+aaaa+aa…a的值,其中a是一個數字。例如2+22+222+2222+22222(此時共有5個數相加),幾個數相加有鍵盤控制。
sum1 = 0
print('請輸入元素的值:')
a = int(input())
list1 = [a]
print('請輸入數的個數:')
n = int(input())
for i in range(1, n):
list1.append(list1[i - 1] * 10 + a)
print(sum(list1))
輸出結果:
請輸入元素的值:
2
請輸入數的個數:
5
24690
- #####打印圖形1
img = '★'
list1 = [img]
print('請輸入圖形行數:')
n = int(input())
for i in range(0, n):
if i == 0:
print(list1[i])
else:
list1.append(list1[i-1]+img)
print(list1[i])
輸出結果:
5
★
★★
★★★
★★★★
★★★★★
- #####打印圖形2
print('請輸入圖形行數:')
n = int(input())
picture = '@'
list1 = [picture]
img = [picture]
for i in range(0, n):
if i == 0:
print(img[i].center(n*2))
else:
list1.append(list1[i - 1] + picture*2)
img.append(list1[i])
print(img[i].center(n*2))
輸出結果:
4
@
@@@
@@@@@
@@@@@@@
- #####打印圖形3
“`Python
img = ‘★’
list1 = [img]
list2 = []
print(‘請輸入圖形行數:’)
n = int(input())
for i in range(0, n):
if i == 0:
continue
else:
list1.append(list1[i-1]+img)
for i in range(0, n):
list2.append(list1[n-1])
n -= 1
print(list2[i])
輸出結果:
4
★★★★
★★★
★★
★
- #####輸入兩個正整數m和n,求其最大公約數和最小公倍數。
```Python
m = int(input('請輸入第一個正整數:'))
n = int(input('請輸入第二個正整數:'))
max_1 = 0
min_1 = 0
for i in range(1, m*n+1):
if (m % i == 0) and (n % i == 0):
max_1 = i
# if (i % m == 0) and (i % n == 0):
# min_1 = i
# break
min_1 =int(m * n / max_1)
print('最大公約數爲:', max_1, '最小公倍數爲:', min_1)
輸出結果:
請輸入第一個正整數:15
請輸入第二個正整數:18
最大公約數爲: 3 最小公倍數爲: 90
<div class="se-preview-section-delimiter"></div>
- #####一個數如果恰好等於它的因子之和,這個數就稱爲 “完數 “。例如6=1+2+3.編程 找出1000以內的所有完數
number = [1]
n = 1001
for j in range(1, n):
number2 = []
for i in range(1, j):
if j % i == 0:
number2.append(i)
a = sum(number2)
if a == j:
number.append(j)
print(number)
輸出結果:
[1, 6, 28, 496]
<div class="se-preview-section-delimiter"></div>
- ##### 輸出9*9⼝口訣
b = []
for i in range(1, 10):
a = []
for j in range(1, i+1):
a.append(i * j)
b = a[:]
print(j, '*', i, '=', b[-1], end=" ")
if i == j:
print('\t')
輸出結果:
1 * 1 = 1
1 * 2 = 2 2 * 2 = 4
1 * 3 = 3 2 * 3 = 6 3 * 3 = 9
1 * 4 = 4 2 * 4 = 8 3 * 4 = 12 4 * 4 = 16
1 * 5 = 5 2 * 5 = 10 3 * 5 = 15 4 * 5 = 20 5 * 5 = 25
1 * 6 = 6 2 * 6 = 12 3 * 6 = 18 4 * 6 = 24 5 * 6 = 30 6 * 6 = 36
1 * 7 = 7 2 * 7 = 14 3 * 7 = 21 4 * 7 = 28 5 * 7 = 35 6 * 7 = 42 7 * 7 = 49
1 * 8 = 8 2 * 8 = 16 3 * 8 = 24 4 * 8 = 32 5 * 8 = 40 6 * 8 = 48 7 * 8 = 56 8 * 8 = 64
1 * 9 = 9 2 * 9 = 18 3 * 9 = 27 4 * 9 = 36 5 * 9 = 45 6 * 9 = 54 7 * 9 = 63 8 * 9 = 72 9 * 9 = 81
<div class="se-preview-section-delimiter"></div>
- #####一個5位數,判斷它是不是迴文數。即12321是迴文數,個位與萬位相同,十位與千位相同
while True:
n = int(input("請輸入一個五位數:"))
s = str(n)
if len(s) == 5:
if s[0] == s[4] and s[1] == s[3]:
print(n, '是一個迴文數')
else:
print(n, '不是一個迴文數')
else:
print("你的輸入有誤,程序退出")
break
輸出結果:
請輸入一個五位數:12321
12321 是一個迴文數
請輸入一個五位數:12345
12345 不是一個迴文數
請輸入一個五位數:1
你的輸入有誤,程序退出
<div class="se-preview-section-delimiter"></div>
- #####打印圖形(菱形)
n = int(input('請輸入圖形行數:'))
picture = '@'
list1 = [picture]
img = [picture]
for i in range(0, n):
if i == 0:
print(img[i].center(n*2))
else:
list1.append(list1[i - 1] + picture*2)
img.append(list1[i])
print(img[i].center(n*2))
while i > 0:
print(img[i-1].center(n * 2))
i -= 1
輸出結果:
請輸入圖形行數:4
@
@@@
@@@@@
@@@@@@@
@@@@@
@@@
@
<div class="se-preview-section-delimiter"></div>
- ##### 輸入一行字符,分別統計出其中英文字母、空格、數字和其它字符的個數。
str1 = input("請輸入一串字符:")
a = []
char = 0
number = 0
blank = 0
other = 0
for i in range(0, len(str1)):
a.append(str1[i])
for j in range(0, len(a)):
if a[j] == ' ':
blank += 1
elif 'a' <= a[j] <= 'z' or 'A' <= a[j] <= 'Z':
char += 1
elif "0" <= a[j] <= '9':
number += 1
else:
other += 1
print('char =', char, 'number =', number, 'blank =', blank, 'other =', other)
輸出結果:
請輸入一串字符:hjsakj2 44hkjbwr3 +fe-w+ +df
char = 17 number = 4 blank = 3 other = 4