python迴圈語句判斷的使用

凱凱要加班發表於2020-12-18

**

python(迴圈語判斷的使用)

**

1.三角形

n = 9
for i in range(n):
for j in range(i):
print(’ ‘, end=’’)
for k in range(i, n):
print(’*’, end=’ ')
print()

2.九九乘法表

for i in range(1, 10):
for n in range(1, i + 1):
print(’%s×%s=%s’ % (i, n, i * n), end=’ ')
print()
3.使用for迴圈輸出數字0-9,當數字為6時,跳出本次迴圈,執行其他迴圈當數字為8的時候,停止迴圈
for i in range(0,10):
if i == 6:
continue
elif i == 8:
break
else:
print(i)
4使用迴圈 計算出1-100之間 所有基數的和,偶數和
s = 0
for i in range(1,100):
if i % 2 == 0:
s += i
if i % 2 !=0:
s +=1

else:

print(s)
偶數和
s = 0
for i in range(1,100):
if i % 2 == 0:
s +=i
print(s)
奇數和
s = 0
for i in range(1,100):
if i % 2!= 0:
s += i
print(s)
5.模擬ATM取錢系統,取款機裡一共有10000元, # 有五個人分別去取錢,每次取錢的金額在100元到500元之間,
要求輸出他們每次取錢的金額,以及ATM機裡的餘額,
當餘額小於取款的金額時,退出系統。

import random
import os
import threading
import time
from threading import Thread, Lock

lock = Lock()
meon = 10000

def a(i):
while True:
global meon
time.sleep(1)
lock.acquire()
mon = random.randint(100, 500)
if mon <= meon:
meon -= mon
print("%s取了%d,剩餘%d" % (i, mon, meon))
else:
print(“餘額不足”)
os._exit(0)
lock.release()

if name == ‘main’:
list = [“張三”, “李四”, “王五”, “趙六”, “王七”]
for i in list:
t = threading.Thread(target=a, args=(i,))
t.start()
from functools import reduce

def fun(i):
return reduce(lambda x, y: x + y, i)

b = fun(eval(input(“輸入列表:”)))
print(b)

相關文章