## 【wk06_1012】迴圈
【inclass practice】
```python
n = 50
for i in range (1, n+1):
if i %5 == 0:
continue
print(i)
```
1
2
3
4
6
7
8
9
11
12
13
14
16
17
18
19
21
22
23
24
26
27
28
29
31
32
33
34
36
37
38
39
41
42
43
44
46
47
48
49
```python
a = 11
b = 57
maxnu = a * b
for i in range(1,maxnu + 1):
if ( i % a == 0 and i % b == 0 ):
ans = i
break
print("最小公倍數:",ans)
```
最小公倍數: 627
```python
num = 3
ans = 1
n = 0
while n <= 5:
n = n+1
ans = ans* n
print("num! = ",ans)
```
num! = 720
實作5:
一個正整數除了1和自己外,無法再被其他數整除,這個數就是質數。 請輸入一正整數,列出此數的所有正因數,並判斷使數字是否為質數
```python
a = 15
p = 1
counter = 0
while p <= a :
if a % p == 0:
counter = counter+1
p = p + 1
if counter > 2 :
ans = "質數"
else :
ans = "合數"
print(a,"質數","合數")
```
15 質數 合數
{補充 while 練習}
猜數字遊戲:寫一個程式,隨機選擇一個1到100的數字,然後要求玩家通過輸入猜測該數字,直到猜對為止。使用while迴圈來實現,並在玩家猜對時結束遊戲。
import random
生成隨機數字
target_number = random.randint(1, 100)
guess = None attempts = 0
while guess != target_number: guess = int(input("請猜一個1到100的數字:")) attempts += 1
if guess < target_number:
print("太低了!再猜一次。")
elif guess > target_number:
print("太高了!再猜一次。")
print(f"恭喜!你猜對了,答案是{target_number}。你總共猜了{attempts}次。")
使用者輸入:寫一個程式,要求使用者輸入一個整數,然後使用while迴圈,只有當輸入的數字是正整數時,才停止請求輸入。如果輸入不是正整數,則繼續要求輸入,直到收到正確的輸入。
user_input = None
while not user_input: user_input = input("請輸入一個正整數:") if user_input.isdigit(): user_input = int(user_input) else: user_input = None print("請輸入正確的整數。")
print(f"您輸入了正整數:{user_input}")
倒數計時器:寫一個程式,請求使用者輸入一個正整數作為倒數計時的秒數,然後使用while迴圈從輸入的秒數倒數到0。在每個秒數的間隔打印出剩餘的秒數。
import time
countdown = int(input("請輸入倒數的秒數:"))
while countdown > 0:
print(countdown)
#
countdown -= 1
print("時間到!")
累積加法:寫一個程式,要求使用者不斷輸入整數,並使用while迴圈計算這些整數的總和。當使用者輸入0時,停止接受輸入並印出總和。
total = 0
while True: num = int(input("請輸入一個整數(輸入0結束):")) if num == 0: break total += num
print(f"總和:{total}")
數字猜謎:寫一個程式,在1到10之間選擇一個隨機數字,然後要求玩家通過輸入猜測該數字。使用while迴圈來實現,並提供猜測是太高還是太低的提示,直到猜對為止。
import random
生成隨機數字
target_number = random.randint(1, 10)
guess = None
while guess != target_number: guess = int(input("請猜一個1到10的數字:"))
if guess < target_number:
print("太低了!再猜一次。")
elif guess > target_number:
print("太高了!再猜一次。")
print(f"恭喜!你猜對了,答案是{target_number}。")
```python
import random
target_number = random.randint(1, 100)
guess = None
attempts = 0
while guess != target_number:
guess = int(input("請猜一個1到100的數字:"))
attempts += 1
if guess < target_number:
print("太低了!再猜一次。")
elif guess > target_number:
print("太高了!再猜一次。")
print(f"恭喜!你猜對了,答案是{target_number}。你總共猜了{attempts}次。")
```
```python
import random
# 生成隨機數字
target_number = random.randint(1, 10)
guess = None
while guess != target_number:
guess = int(input("請猜一個1到10的數字:"))
if guess < target_number:
print("太低了!再猜一次。")
elif guess > target_number:
print("太高了!再猜一次。")
print(f"恭喜!你猜對了,答案是{target_number}。")
```
```python
import time
countdown = int(input("請輸入倒數的秒數:"))
while countdown > 0:
print(countdown)
#############
countdown -= 1
print("時間到!")
```
```python
total = 0
while True:
num = int(input("請輸入一個整數(輸入0結束):"))
if num == 0:
break
total += num
print(f"總和:{total}")
```
```python
```
```python
! pip install requests beautifulsoup4
```
```python
【afterclass practice】
複習ch03、ch04 教學影音 lesson 7、9
```
```python
pip install pygame
```
Defaulting to user installation because normal site-packages is not writeable
Collecting pygame
Downloading pygame-2.5.2-cp310-cp310-win_amd64.whl (10.8 MB)
--------------------------------------- 10.8/10.8 MB 23.4 MB/s eta 0:00:00
Installing collected packages: pygame
Successfully installed pygame-2.5.2
Note: you may need to restart the kernel to use updated packages.
```python
import pygame
# 初始化
pygame.init()
# 设置窗口标题
screencaption=pygame.display.set_caption('Gobang')
# 设置大小
screen=pygame.display.set_mode([350,285])
# 初始化字体
myfont=pygame.font.Font(None,30)
textImage=myfont.render("Hello Pygame",True,[255,255,255])
screen.blit(textImage,(100,100))
# 棋子状态0为空 1为白色 2为黑色
status_list = {}
for i in range(0, 15*18):
status_list[i] = 0
#print(status_list)
clock = pygame.time.Clock()
# 0 是白棋走 1是黑棋走
flag = 0
# 将要绘制的棋子的位置
movex = 1
movey = 1
while True:
clock.tick(30)
# 绘制棋盘
screen.fill([255,255,255])
for i in range(0, 15):
pygame.draw.line(screen,[0,0,0],[0,i*20],[280,i*20],2)
for i in range(0, 15):
pygame.draw.line(screen,[0,0,0],[i*20,0],[i*20,280],2)
# 绘制棋子
for x in range(0, 15):
for y in range(0, 15):
if status_list[x*15 + y] == 1:
pygame.draw.circle(screen,[255,0,0],[ 2 + y * 20,2 + x*20],10)
elif status_list[x*15 + y] == 2:
pygame.draw.circle(screen,[0,0,0],[ 2 + y * 20, 2 + x*20],10)
# 判断是否获胜
# X轴的判定
if y < 11:
# 白棋获胜
if status_list[x*15 + y] == 1 and status_list[x*15 + y + 1] == 1 and status_list[x*15 + y + 2] == 1 and status_list[x*15 + y + 3] == 1 and status_list[x*15 + y + 4] == 1:
print("白棋胜利")
# break
# 黑棋获胜
if status_list[x*15 + y] == 2 and status_list[x*15 + y + 1] == 2 and status_list[x*15 + y + 2] == 2 and status_list[x*15 + y + 3] == 2 and status_list[x*15 + y + 4] == 2:
print("黑棋胜利")
# break
# 判断是否获胜
# Y轴的判定
if x < 11:
if status_list[x*15 + y] == 1 and status_list[(x+1)*15 + y] == 1 and status_list[(x+2)*15 + y] == 1 and status_list[(x+3)*15 + y] == 1 and status_list[(x+4)*15 + y] == 1:
print("白棋胜利")
# break
if status_list[x*15 + y] == 2 and status_list[(x+1)*15 + y] == 2 and status_list[(x+2)*15 + y] == 2 and status_list[(x+3)*15 + y] == 2 and status_list[(x+4)*15 + y] == 2:
print("黑棋胜利")
# break
# 判断是否获胜
# 斜着判断 正对角线
if status_list[x*15 + y] == 1 and status_list[(x+1)*15 + (y+1)] == 1 and status_list[(x+2)*15 + (y+2)] == 1 and status_list[(x+3)*15 + (y+3)] == 1 and status_list[(x+4)*15 + (y+4)] == 1:
print("白棋胜利")
# break
if status_list[x*15 + y] == 2 and status_list[(x+1)*15 + (y+1)] == 2 and status_list[(x+2)*15 + (y+2)] == 2 and status_list[(x+3)*15 + (y+3)] == 2 and status_list[(x+4)*15 + (y+4)] == 2:
print("黑棋胜利")
# break
# 判断是否获胜
# 斜着判断 反对角线
if status_list[x*15 + y] == 1 and status_list[(x+1)*15 + (y-1)] == 1 and status_list[(x+2)*15 + (y-2)] == 1 and status_list[(x+3)*15 + (y-3)] == 1 and status_list[(x+4)*15 + (y-4)] == 1:
print("白棋胜利")
# break
if status_list[x*15 + y] == 2 and status_list[(x+1)*15 + (y-1)] == 2 and status_list[(x+2)*15 + (y-2)] == 2 and status_list[(x+3)*15 + (y-3)] == 2 and status_list[(x+4)*15 + (y-4)] == 2:
print("黑棋胜利")
# break
# 绘制落棋位置
pygame.draw.circle(screen,[0,0,0],[ 2 + movex*20, 2 + movey*20],10,3)
# 绘制文字 显示到谁落棋子
if flag == 0:
textImage=myfont.render("White",True,[255,0,0])
else:
textImage=myfont.render("Black",True,[0,0,255])
screen.blit(textImage,(290,10))
# 判断事件
for event in pygame.event.get():
# 退出事件
if event.type==pygame.QUIT:
pygame.quit()
quit()
# 键盘事件
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
if movex > 0:
movex = movex - 1
if event.key == pygame.K_RIGHT:
if movex < 14:
movex = movex + 1
if event.key == pygame.K_UP:
if movey > 0:
movey = movey - 1
if event.key == pygame.K_DOWN:
if movey < 14:
movey = movey + 1
if event.key == pygame.K_SPACE:
if flag == 0:
if status_list[movey * 15 + movex] == 0:
status_list[movey * 15 + movex] = 1
flag = 1
elif flag == 1:
if status_list[movey * 15 + movex] == 0:
status_list[movey * 15 + movex] = 2
flag = 0
# 刷新页面
pygame.display.flip()
print("Done!")
```
pygame 2.5.2 (SDL 2.28.3, Python 3.10.9)
Hello from the pygame community. https://www.pygame.org/contribute.html