pyshon第二天

分支结构

f语句的使用

在Python中,要构造分支结构可以使用ifelifelse关键字。所谓关键字就是有特殊含义的单词,像ifelse就是专门用于构造分支结构的关键字,很显然你不能够使用它作为变量名(事实上,用作其他的标识符也是不可以)。

username = input('请输入用户名: ')
password = input('请输入口令: ')
# 如果希望输入口令时 终端中没有回显 可以使用getpass模块的getpass函数
# import getpass
# password = getpass.getpass('请输入口令: ')
# [ conditions ]
if username == 'admin' and password == '123456':
print('身份验证成功!')
# Other...
else:
print('身份验证失败!')

# 案例,有数字大写字母和小写字母
password = 'A12'
A = 'ZXCVBNMASDFGHJKLQWERTYUIOP'
B ='zxcvbnmasdfghjklqwertyuiop'
C = '1234567890'

count1,count2,count3 = False,False,False
for i in password:
if i in A:
count1 = True
if i in B:
count2 = True
if i in C:
count3 = True
if count1 and count2 and count3:
print('OK')
else:
print('必须含有大写小写和数字')

x = float(input('x = '))
if x > 1:
y = 3 * x - 5
elif x >= -1:
y = x + 2
else:
y = 5 * x + 3
print('f(%.2f) = %.2f' % (x, y))


# 加减乘除

num1,num2 = map(float,input('Num1,Num2').split(','))
choose_method = input('Choose Method:[+,-,*,/]')

if choose_method in '+-*/':
if choose_method == "+":
print('%.2f + %.2f = %.2f'%(num1,num2,num1 + num2))
elif choose_method == '-':
print('%.2f - %.2f = %.2f'%(num1,num2,num1 - num2))
elif choose_method == '*':
print('%.2f * %.2f = %.2f'%(num1,num2,num1 * num2))
else:
print('%.2f / %.2f = %.2f'%(num1,num2,num1 /num2))
else:
# 抛出错误
raise KeyError('Only choose [+,-,*,/]')

练习

练习1:英制单位与公制单位互换

value = float(input('请输入长度: '))
unit = input('请输入单位: ')
if unit == 'in' or unit == '英寸':
print('%f英寸 = %f厘米' % (value, value * 2.54))
elif unit == 'cm' or unit == '厘米':
print('%f厘米 = %f英寸' % (value, value / 2.54))
else:
print('请输入有效的单位')

练习2:掷骰子决定做什么

from random import randint

face = randint(1, 6)
if face == 1:
result = '唱首歌'
elif face == 2:
result = '跳个舞'
elif face == 3:
result = '学狗叫'
elif face == 4:
result = '做俯卧撑'
elif face == 5:
result = '念绕口令'
else:
result = '讲冷笑话'
print(result)

练习3:百分制成绩转等级制

score = float(input('请输入成绩: '))
if score >= 90:
grade = 'A'
elif score >= 80:
grade = 'B'
elif score >= 70:
grade = 'C'
elif score >= 60:
grade = 'D'
else:
grade = 'E'
print('对应的等级是:', grade)

练习4:输入三条边长如果能构成三角形就计算周长和面积

import math

a = float(input('a = '))
b = float(input('b = '))
c = float(input('c = '))
if a + b > c and a + c > b and b + c > a:
print('周长: %f' % (a + b + c))
p = (a + b + c) / 2
area = math.sqrt(p * (p - a) * (p - b) * (p - c))
print('面积: %f' % (area))
else:
print('不能构成三角形')

练习5:个人所得税计算器。


salary = float(input('本月收入: '))
insurance = float(input('五险一金: '))
diff = salary - insurance - 3500
if diff <= 0:
rate = 0
deduction = 0
elif diff < 1500:
rate = 0.03
deduction = 0
elif diff < 4500:
rate = 0.1
deduction = 105
elif diff < 9000:
rate = 0.2
deduction = 555
elif diff < 35000:
rate = 0.25
deduction = 1005
elif diff < 55000:
rate = 0.3
deduction = 2755
elif diff < 80000:
rate = 0.35
deduction = 5505
else:
rate = 0.45
deduction = 13505
tax = abs(diff * rate - deduction)
print('个人所得税: ¥%.2f元' % tax)
print('实际到手收入: ¥%.2f元' % (diff + 3500 - tax))

for-in循环

如果明确的知道循环执行的次数或者是要对一个容器进行迭代(后面会讲到),那么我们推荐使用for-in循环,例如下面代码中计算$sum_{n=1}^{100}n$。

sum = 0
# 用于设定for循环的迭代次数,
# range 也是一个前闭后开
# 可迭代对象
for x in range(101):
sum += x
print(sum)

需要说明的是上面代码中的range类型,range可以用来产生一个不变的数值序列,而且这个序列通常都是用在循环中的,例如:

  • range(101)可以产生一个0到100的整数序列。

  • range(1, 100)可以产生一个1到99的整数序列。

  • range(1, 100, 2)可以产生一个1到99的奇数序列,其中的2是步长,即数值序列的增量。

知道了这一点,我们可以用下面的代码来实现1~100之间的偶数求和。

while循环

如果要构造不知道具体循环次数的循环结构,我们推荐使用while循环,while循环通过一个能够产生或转换出bool值的表达式来控制循环,表达式的值为True循环继续,表达式的值为False循环结束。下面我们通过一个“猜数字”的小游戏(计算机出一个1~100之间的随机数,人输入自己猜的数字,计算机给出对应的提示信息,直到人猜出计算机出的数字)来看看如何使用while循环。

猜数字游戏
计算机出一个1~100之间的随机数由人来猜
计算机根据人猜的数字分别给出提示大一点/小一点/猜对了

import random

answer = random.randint(1, 100)
counter = 0
while True:
counter += 1
number = int(input('请输入: '))
if number < answer:
print('大一点')
elif number > answer:
print('小一点')
else:
print('恭喜你猜对了!')
break
print('你总共猜了%d次' % counter)
if counter > 7:
print('你的智商余额明显不足')

练习

练习1:输入一个数判断是不是素数。

from math import sqrt

num = int(input('请输入一个正整数: '))
end = int(sqrt(num))
is_prime = True
for x in range(2, end + 1):
if num % x == 0:
is_prime = False
break
if is_prime and num != 1:
print('%d是素数' % num)
else:
print('%d不是素数' % num)

练习2:输入两个正整数,计算最大公约数和最小公倍数。

x = int(input('x = '))
y = int(input('y = '))
if x > y:
x, y = y, x
for factor in range(x, 0, -1):
if x % factor == 0 and y % factor == 0:
print('%d和%d的最大公约数是%d' % (x, y, factor))
print('%d和%d的最小公倍数是%d' % (x, y, x * y // factor))
break

练习3 银行卡密码

ini_passward = 100000
input_ = int(input('请输入密码>>:'))

for i in range(2):
if input_ == ini_passward:
print('OK')
break
else:
print('密码错误,请尝试重新输入')
input_ = int(input('请输入密码>>:'))
else:
print('账号锁定,请移步至柜台你个傻X..')

练习4 验证码

import random
# 随机产生给予范围之内的随机数字.


for i in range(3):
yanzhengma = random.randrange(1000,9999)
print('验证码为: %d' % yanzhengma)
input_ = int(input('请输入验证码>>:'))
if input_ == yanzhengma:
print('欢迎您,您真是一个小天才.')
break
else:
print('验证码错误,请尝试重新输入')

原文地址:https://www.cnblogs.com/duyunlong123456/p/11278905.html