Python —循环分支语句

  

 


## 分支结构 -- if语句


```python
import getpass
username = input ('请输入用户名')
#password = input('请输入密码:')  明文
password = getpass.getpass('请输入密码:')  #密文
if username == 'admin' and password == '123456':
    print('欢迎使用本系统')#  缩进4个空格
    print('开始运行APP')
else:
    print('用户名或密码错误!')
print('今天天气真好')# 不受if-else 控制
```

## elif


```python
"""
f(x) = 5x + 3 (x < -1 )
       2x - 1 (-1 <= x <= 1)
       3x - 5 (x > 1)


"""
x = float(input('x = '))
if x < -1:
    y = 5 * x + 3 
elif x <= 1:
    y = 2 * x - 1
else:
    y = 3 * x -5
print('y =',y)
```


百分制转换成等级制 90以上A   80至90 B 70至80 C 60至70 D 其余是E


```python
score = float(input('请输入成绩'))
if score < 60:
    print('成绩为E')
elif score < 70:
    print('成绩为D')
elif score < 80:
    print('成绩为C')
elif score < 90:
    print('成绩为B')
else:
    print('成绩为A')
```


下面这种嵌套的if...else 的写法是我们不推荐使用的,因为官方的建议是扁平优于嵌套




```python
score = float(input('请输入成绩'))
if score >= 90:
    print('A')
else:
    if score >= 80:
        print('B')
    else:
        if score >= 70:
            print('C')
        else:
            if score >= 60:
                print('D')
            else:
                print('E')
        
```


掷骰子 1.
随机数random[0,1)




```python
import random
print( random.random())
```




```python
from random import random ,randint  #从random中导入random
from math import pi , sqrt #从math中导入pi, sqrt
print(random())
print(random() * 200 -100)#[-100,100)
print(randint(1,6))#随机生成[1,6]的随机数
```




```python
from random import randint
r  =  randint(1,6)
if r == 1:
    print('a')
elif r == 2:
    print('b')
elif r == 3:
    print('c')
elif r == 4:
    print('d')
elif r == 5:
    print('e')
else:
    print('f')
```


1.顺序结构
2.分支结构
3.循环结构


## 循环结构




```python
from random import randint
f1 = f2 = f3 = f4 = f5 = f6 = 0 
"""
f1 = 0
f2 = 0
f3 = 0
f4 = 0
f5 = 0
f6 = 0
"""
for x in range(60000): #执行60000次
    
    r  =  randint(1,6)
    if r == 1:
        f1 += 1
    elif r == 2:
        f2 += 1
    elif r == 3:
        f3 += 1
    elif r == 4:
        f4 += 1
    elif r == 5:
        f5 += 1
    else:
        f6 += 1
print('1点摇出了%d次' % f1 )
print('2点摇出了%d次' % f2 )
print('3点摇出了%d次' % f3 )
print('4点摇出了%d次' % f4 )
print('5点摇出了%d次' % f5 )
print('6点摇出了%d次' % f6 )
```


pip install pycodestyle 检查工具
url统一资源定位符




```python
for x  in range(1000):  #   x表示从0至 999      _同样是变量,默认不用
    print('hello world')
#  print('hello world! \n' * 1000)  同样效果
```




```python
\ 转义字符
\n 换行
```




```python
print(12,'12',sep = ':') # 中间以:分隔
```


    12:12
    




```python
import time
for num in range(10):
    print(num,'hello, world',sep=':')
    time.sleep(1)# 间隔一秒
```


    0:hello, world
    1:hello, world
    2:hello, world
    3:hello, world
    4:hello, world
    5:hello, world
    6:hello, world
    7:hello, world
    8:hello, world
    9:hello, world
    


 1 到 100求和




```python
my_sum = 0
for a in range(1,101):
    my_sum += a
print(my_sum)
    
```


    5050
    




```python
my_sum = 0
for a in range(1,101):
    if a % 2 == 0:
        my_sum += a
    else:
        pass  # 跳过,else可省略,
print(my_sum)
        


```


    2550
    




```python
my_sum = 0
for a in range(2,101,2):
    my_sum += a
print(my_sum)
    
```


    2550
    


1至100间 3或5的倍数 之和




```python
my_sum = 0
for x in range(101):
    if x % 3 ==0 or x % 5 == 0:
        my_sum += x
print(my_sum)


```


    2418
    


## while 语句




```python
num = 0
while num < 10:
    print('hello, world')
    num += 1


```


    hello, world
    hello, world
    hello, world
    hello, world
    hello, world
    hello, world
    hello, world
    hello, world
    hello, world
    hello, world
    




```python
my_sum = 0
num = 0
while num < 101:
    if num % 3 == 0 or num % 5 == 0:
        my_sum += num
    num += 1
print(my_sum)
```


猜数字,计算机随机出个数,人来猜,猜错了提示猜的过大或过小,猜对结束,猜错7次以上,智商捉急




```python
import random
num = 1
r = random.randint(1,100)
x = int(input('请输入1-100中的任意数字'))
while x != r:
    if x > r:
        x = int(input('您输入的数字偏大,请重新输入'))  
    else:
        x = int(input('您输入的数字偏小,请重新输入'))
    num += 1
    if num > 7:
        print('您的智商真着急')
print('恭喜您猜对了')
```




```python
from random import randint
answer + randint(1,100)
counter = 0
while True:
    your_answer = int(int('请输入'))
    counter += 1
    if your_answer < answer:
        print('大一点')
    elif your_answer > answer:
        print('小一点')
    else:
        print('恭喜您猜对了')
        break
if counter > 7:
    print('智商捉急')
```

 

 

# ### 复杂小乌龟



# In[6]:




import turtle
turtle.bgcolor('black')
turtle.pencolor('red')
turtle.fillcolor('red')
turtle.begin_fill()
turtle.pensize(width=8)
turtle.forward(300)
turtle.left(120)
turtle.forward(300)
turtle.left(120)
turtle.forward(300)
turtle.right(120)
turtle.end_fill()
turtle.pencolor('white')
for _ in range(60):
    turtle.right(1)
    turtle.forward(3)
turtle.pencolor('red')
turtle.begin_fill()
turtle.right(60)
turtle.forward(300)
turtle.right(120)
turtle.forward(300)
turtle.right(120)
turtle.forward(300)
turtle.right(60)
turtle.end_fill()
turtle.pencolor('white')


for _ in range(300):
    turtle.right(1)
    turtle.forward(3)


turtle.mainloop()




# ### 反转猜数字,人出数字,计算机猜


# In[1]:




import random
answer = ''
r = random.randint(1,100)


max = 100
min = 1
while answer != '猜对了':
    if answer == '偏大':
        max = r - 1
        r = random.randint(min,max)
    elif answer == '偏小':
        min = r + 1
        r = random.randint(min,max)
    print('我猜是',r)
    answer = input('')
print('我厉害吧')




# ### 人机猜拳 人机各有1000元,计算机产生随机数,可以下注,谁先输光谁输


# In[17]:




from random import randint
my = ''
m = 0
pd = 0
m_zzc = 1000
c_zzc = 1000
n = 0
while (m_zzc > 0) and (c_zzc > 0):
    dz = int(input('请下注,您目前剩余资产为%d 元,电脑剩余资产为%d元,您的下注为'% (m_zzc,c_zzc)))
    my = input('猜拳开始')
    if my == '石头':
        m = 1
    elif my == '剪刀':
        m = 2
    elif my == '布':
        m = 3 
    else:
        my = input('输入有误请重新输入')
    you = randint(1,3)
    if m == 1 and  you == 1:
        print('平局')
        pd = 0
    elif m == 1 and you == 2:
        print('你赢了')
        pd = 1
    elif m == 1 and you == 3:
        print('电脑胜')
        pd = 2
    elif m == 2 and you == 1:
        print('电脑胜')
        pd = 2
    elif m == 2 and you == 2:
        print('平局')
        pd = 0
    elif m == 2 and you == 3:
        print('你赢了')
        pd = 1
    elif m == 3 and you == 1:
        print('你赢了')
        pd = 1
    elif m == 3 and you == 2:
        print('电脑胜')
        pd = 2
    elif m == 3 and you == 3:
        print('平局')
        pd = 0
    if pd == 1:
         m_zzc +=dz
         c_zzc -=dz
    elif pd == 2:
         m_zzc -=dz
         c_zzc +=dz
if m_zzc == 0:
    print('您已出局')
else:
    print('您赢了电脑')
    




# ### 个人所得税 总收入 — 五险一金 (输入固定的额)> 3500 税 = (总收入 - 五险一金 - 3500) * 税率 -速算扣除数


# In[5]:




my_salary = float(input('请输入您的工资'))
my_wx = float(input('请输入您要缴纳五险一金的值'))
my_tax = 0
c = 0
if (my_salary - my_wx)<= 3500:
    my_tax = 0
else:
    c = my_salary - 3500 - my_wx
    if c <= 1500:
        my_tax = c * 0.03
    elif c<= 4500:
        my_tax = c * 0.1 - 105
    elif c <= 9000:
        my_tax = c * 0.2 - 555
    elif c <= 35000:
        my_tax = c * 0.25 - 1005
    elif c <= 55000:
        my_tax = c * 0.3 - 2775
    elif c <= 80000:
        my_tax = c * 0.35 - 5505
    else:
        my_tax = c * 0.45 - 13505
print('您应缴纳税款%.2f 元,税后工资为%.2f'%(my_tax,my_salary - my_tax - my_wx))
        
    

 

 

你可能感兴趣的:(Python —循环分支语句)