---
# System prepended metadata

title: Python#1
tags: [if, 迴圈, python]

---

# Python#1

[TOC]

**課程ppt**
<iframe src="https://drive.google.com/file/d/1cZ4W94KE4DXiV6sYnwOrfGUWm1KenBrx/preview" width="640" height="480"></iframe>

## 變數
- 變數名稱不能為數字開頭

## 判斷式

### if
```python
score = int(input("請輸入成績"))
if score >=60:
    print("成績及格!")
```
### if else
```python
score = int(intput("score"))
if score >=60:
    print("及格")
else:
    print("不及格!")
```
### elif
```python
score = int(intput("score"))
if score >=90:
    print("a")
elif socre >=80:
    print("b")
elif score >=70:
    print("c")
else:
    print("不及格!")
```

:::info
在python中，**else** 一定要加上
:::

## 迴圈

### for
```python
times=5
for i int range(times):
    print(i)
```

```python=
for i in range(0,12,2):
    print(i)
```
### while
```python
while True:
user = input('Hellow')
    if user == 'Stop':
        break
    if user == '不要學了':
        continue
    print(user)
print("ok,finish")
```
### 其他用法
從尾端印回來
**len(cmp)** 為取長度，如果是陣列的話可以直接放入迴圈裡不用加上**len**
```python=
cmp = ['123','456','789','333']
for i int reversed(cmp):
    print(i)
```

## 亂數
```python=
import random
random.randint(0,100)

random.randrange(0,100,2)

```

## 課堂練習
```python=
import random
answer = random.randint(0,1000)
maxnumber=1000
minnumber=0
for guesscount in range(6):
    print("第",guesscount+1,"次")
    guess = input()
    guess = int(guess)
    
    if guess < answer:
        minnumber = guess
        print("range",minnumber,"~",maxnumber)      
    elif guess > answer:
        maxnumber = guess
        print("range",minnumber,"~",maxnumber)
    elif guess == answer:
        print("恭喜你猜中")
        break;
print("結束")
```
###### tags: `python` `if` `迴圈`




