Python练习题 循环语句(37~54)二
程序员文章站
2022-04-21 10:22:38
Demo43**python (使用循环显示四种模式)使用嵌套循环在四个独立的程序中显示下面四种模式。**程序编辑:print("模式A")while True: for i in range (1,7): for j in range(1, 7): if j <= i: print(j,end="\t") print() breakprint()print("模式B")whi...
Demo43
**
python (使用循环显示四种模式)使用嵌套循环在四个独立的程序中显示下面四种模式。
**
程序编辑:
print("模式A")
while True:
for i in range (1,7):
for j in range(1, 7):
if j <= i:
print(j,end="\t")
print()
break
print()
print("模式B")
while True:
for i in range (6,0,-1):
for j in range(1, 7):
if j <= i:
print(j,end="\t")
print()
break
print()
print("模式C")
while True:
for i in range (1,7):
for j in range(6, 0,-1):
if j <= i:
print(j,end="\t")
else :
print("",end= "\t")
print()
break
print()
print("模式D")
while True:
for i in range (6,0,-1):
a = 6 - i
print("\t" * a,end ="")
for j in range(1, 7):
if j <= i:
print(j,end="\t")
print()
break
print()
测试:
J:\python\python.exe K:/python2020.8.4练习/PythonCode/day1/day03/43.py
模式A
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
模式B
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
模式C
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
6 5 4 3 2 1
模式D
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
Process finished with exit code 0
Demo44
**** ##python (在金字塔模式中显示数字)编写一个嵌套for循环来显示下面的输出。**
**
程序编辑:
while True :
number = int(input("Enter a number:"))
for i in range(0, number):
for k in range(number - i, 0, -1):
print("",end="\t")
for j in range(1,i + 1):
print(2 ** (j-1), end="\t")
for k in range(i + 1,0,-1):
print(2 ** (k-1),end="\t")
print()
测试:
J:\python\python.exe K:/python2020.8.4练习/PythonCode/day1/day03/44.py
Enter a number:8
1
1 2 1
1 2 4 2 1
1 2 4 8 4 2 1
1 2 4 8 16 8 4 2 1
1 2 4 8 16 32 16 8 4 2 1
1 2 4 8 16 32 64 32 16 8 4 2 1
1 2 4 8 16 32 64 128 64 32 16 8 4 2 1
Enter a number:10
1
1 2 1
1 2 4 2 1
1 2 4 8 4 2 1
1 2 4 8 16 8 4 2 1
1 2 4 8 16 32 16 8 4 2 1
1 2 4 8 16 32 64 32 16 8 4 2 1
1 2 4 8 16 32 64 128 64 32 16 8 4 2 1
1 2 4 8 16 32 64 128 256 128 64 32 16 8 4 2 1
1 2 4 8 16 32 64 128 256 512 256 128 64 32 16 8 4 2 1
Enter a number:4
1
1 2 1
1 2 4 2 1
1 2 4 8 4 2 1
Enter a number:
Demo45
**
python (显示在2和1000之间的素数)修改程序清单5-13.显示在2和1000之间且包括2和1000的 素数,每行显示8个素数。
程序编辑:
count = 0
for num in range(2,1001):
for j in range(2, num):
if num % j == 0:
break;
else:
print(num,end = "\t")
count += 1
if count % 8 == 0:
print()
测试:
J:\python\python.exe K:/python2020.8.4练习/PythonCode/day1/day03/45.py
2 3 5 7 11 13 17 19
23 29 31 37 41 43 47 53
59 61 67 71 73 79 83 89
97 101 103 107 109 113 127 131
137 139 149 151 157 163 167 173
179 181 191 193 197 199 211 223
227 229 233 239 241 251 257 263
269 271 277 281 283 293 307 311
313 317 331 337 347 349 353 359
367 373 379 383 389 397 401 409
419 421 431 433 439 443 449 457
461 463 467 479 487 491 499 503
509 521 523 541 547 557 563 569
571 577 587 593 599 601 607 613
617 619 631 641 643 647 653 659
661 673 677 683 691 701 709 719
727 733 739 743 751 757 761 769
773 787 797 809 811 821 823 827
829 839 853 857 859 863 877 881
883 887 907 911 919 929 937 941
947 953 967 971 977 983 991 997
Process finished with exit code 0
**
Demo46
**
python (计算π)你可以使用下面的数列近似计算π。
**
编写程序显示当i=10000、20 000、…100 000时π的值。
程序编辑:
for j in range(10000,100001,10000):
sum = 0
for i in range(1,j + 1):
sum += ((-1) ** (i + 1)) / (2 * i -1)
PI = 4 * sum
print("i = %d 时, PI = %.9f"%(j,PI))
测试:
J:\python\python.exe K:/python2020.8.4练习/PythonCode/day1/day03/46.py
i = 10000 时, PI = 3.141492654
i = 20000 时, PI = 3.141542654
i = 30000 时, PI = 3.141559320
i = 40000 时, PI = 3.141567654
i = 50000 时, PI = 3.141572654
i = 60000 时, PI = 3.141575987
i = 70000 时, PI = 3.141578368
i = 80000 时, PI = 3.141580154
i = 90000 时, PI = 3.141581542
i = 100000 时, PI = 3.141582654
Process finished with exit code 0
Demo47
**
python (计算e)你可以使用下面的数列近似计算e。
**
程序编辑:
for j in range(10000,100001,10000):
e = 1
item = 1
for i in range(1,j + 1):
item *= i #item的阶乘 item = item * i
e += 1 / item
print("i = %d 时, e = %.19f"%(j,e))
测试:
J:\python\python.exe K:/python2020.8.4练习/PythonCode/day1/day03/47.py
i = 10000 时, e = 2.7182818284590455349
i = 20000 时, e = 2.7182818284590455349
i = 30000 时, e = 2.7182818284590455349
i = 40000 时, e = 2.7182818284590455349
i = 50000 时, e = 2.7182818284590455349
i = 60000 时, e = 2.7182818284590455349
i = 70000 时, e = 2.7182818284590455349
i = 80000 时, e = 2.7182818284590455349
i = 90000 时, e = 2.7182818284590455349
i = 100000 时, e = 2.7182818284590455349
Process finished with exit code 0
Demo48
python (完全数)如果一-个正整数等于除了它本身之外所有正因子的和,那么这个数被称为完全数。例 如,6是第一个完全数,因为6=3+2+1。下一个完全数是28=14+7+4+2+1。小于10 000的完全 数有四个。编写程序找出这四个数。
程序编辑:
for i in range(1,10001):
sum = 0
for j in range (1,i):
if i % j == 0:
sum +=j
if i == sum:
print(i)
测试:
J:\python\python.exe K:/python2020.8.4练习/PythonCode/day1/day03/48.py
6
28
496
8128
Process finished with exit code 0
Demo49
**
python (游戏:石头、剪刀、布)编程题4.17给出玩石头、剪刀、布游戏的程序。改写程序让用户不断 玩直到用户或计算机中的某一方能够赢得游戏超过两次。
**
程序编辑:
import random
user_sum = 0
com_sum = 0
while True :
com = random.randint(0,2)
user = int(input("请输入石头(0),剪刀(1),布(2)所对应的数字:"))
if user - com == -1 or user - com == 2: # 0 - 1 ,1 - 2 or 2 - 0
user_sum += 1
if user_sum == 2:
print("玩家赢了")
break
elif user - com == -2 or user - com == 1: # 0 - 2 or 1 - 0, 2 - 1
com_sum += 1
if com_sum == 2:
print("电脑赢了")
break
# else : # (user - com == 0) 0 - 0, 1 - 1 ,2 - 2
# print("平局")
测试:
J:\python\python.exe K:/python2020.8.4练习/PythonCode/day1/day03/49.py
请输入石头(0),剪刀(1),布(2)所对应的数字:1
请输入石头(0),剪刀(1),布(2)所对应的数字:0
请输入石头(0),剪刀(1),布(2)所对应的数字:1
玩家赢了
Process finished with exit code 0
本文地址:https://blog.csdn.net/qq_38891369/article/details/107877321
上一篇: mysql 主键自增
下一篇: 仿“拼多多”下单购买业务实现