Study python_02
2024-10-21 06:10:10
分支结构
简单的使用if语句
使用if-else
import random
# 调用一个随机数包(只看if的情况可忽略)
n1 = random.randrange(100)
n2 = random.randrange(100)
print(n1,n2)
jg = int(input())
if jg == (n1 + n2):
print('结果为真')
else:
print('结果为假')
简单的使用if语句
使用if-elif-else
a,b,c = map(float,input("Enter a,b,c : ").split())# 连续输入三个数字
i = b * b - (4 * a * c)
if i >0 :
r1 = (-b + (b * b - (4 * a * c))** 0.5) / 2 * a
r2 = (-b - (b * b - (4 * a * c))** 0.5) / 2 * a
print("The roots are {} and {}",r1,r2)
elif i == 0 :# 使用elif再次判断
print(-b / 2 * a)
else :# 使用else进行最后的输出
print("The equation has no real roots")
循环结构
简单的使用for语句
password = input('passward:')# 输入一串字符,必须包括数字,大小写字母
count1,count2,count3 = False,False,False
for i in password:# 从第一个字母开始判断
if i <= 'Z' and i >='A':
count1 = True
if i <= 'z' and i >='a':
count2 = True
if i <= '9' and i >='0':
count3 = True
if count1 and count2 and count3:
print('ok')
else:
print('必须包含大小写和数字')
简单的使用while语句
i = 0
while i<10:
print('我是大牛')
i += 1
使用break
import random
c = random.randrange(1000,9999)
print('验证码:',c)
for i in range(3):
b = int(input('请输入验证码:'))# 结果不要应该有四次输出,懒得改
if b == c:
print('输入正确')
break
else:
print('输入错误,请重试')
else:
print('请退出账号重试')
有点头疼的for和if-elif
import numpy as np
A = '石头'
B = '剪刀'
C = '布'
for i in range(6):# 循环6次
res = np.random.choice([A,B,C])
res1 = np.random.choice([A,B,C])
print('电脑出的:',res,'你出的',res1)
if (res == A) and (res1 == B):
print('你输了')
elif (res == A) and (res1 == C):
print('你赢了')
elif (res == B) and (res1 == C):
print('你输了')
elif (res == B) and (res1 == A):
print('你赢了')
elif (res == C) and (res1 == A):
print('你输了')
elif (res == C) and (res1 == B):
print('你赢了')
elif (res == A) and (res1 == A):
print('平局')
elif (res == B) and (res1 == B):
print('平局')
elif (res == C) and (res1 == C):
print('平局')
最新文章
- Qt在ui中使用代码添加新的控件
- vue小总结
- 老周的ABP框架系列教程 -》 一、框架理论初步学习
- LNMP下防跨站、跨目录安全设置,仅支持PHP 5.3.3以上版本
- Big Data Analytics for Security(Big Data Analytics for Security Intelligence)
- javascript eval和JSON之间的联系(转)
- 给hexo添加评论系统
- 【CSS3】使用CSS3线性渐变实现图片闪光划过效果(转)
- 如何使用mysql命令行
- WPF: 只读依赖属性的介绍与实践
- 【读书笔记】iOS-属性中的内存管理参数
- apache、nginx的虚拟域名配置和rewrite配置,以及web缓存的几种方式
- Linux虚拟内存的添加
- [ English ] 俚语 “Ping me=打我电话”
- 使用djcelery发送邮件
- 什么是UML?分哪两类?
- git基本操作:使用git将本地代码上传到GitHub
- BZOJ3884: 上帝与集合的正确用法 拓展欧拉定理
- 专访周金可:我们更倾向于Greenplum来解决数据倾斜的问题
- c++中堆、栈内存分配