分支结构

简单的使用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('平局')

最新文章

  1. Qt在ui中使用代码添加新的控件
  2. vue小总结
  3. 老周的ABP框架系列教程 -》 一、框架理论初步学习
  4. LNMP下防跨站、跨目录安全设置,仅支持PHP 5.3.3以上版本
  5. Big Data Analytics for Security(Big Data Analytics for Security Intelligence)
  6. javascript eval和JSON之间的联系(转)
  7. 给hexo添加评论系统
  8. 【CSS3】使用CSS3线性渐变实现图片闪光划过效果(转)
  9. 如何使用mysql命令行
  10. WPF: 只读依赖属性的介绍与实践
  11. 【读书笔记】iOS-属性中的内存管理参数
  12. apache、nginx的虚拟域名配置和rewrite配置,以及web缓存的几种方式
  13. Linux虚拟内存的添加
  14. [ English ] 俚语 “Ping me=打我电话”
  15. 使用djcelery发送邮件
  16. 什么是UML?分哪两类?
  17. git基本操作:使用git将本地代码上传到GitHub
  18. BZOJ3884: 上帝与集合的正确用法 拓展欧拉定理
  19. 专访周金可:我们更倾向于Greenplum来解决数据倾斜的问题
  20. c++中堆、栈内存分配

热门文章

  1. Kubernetes--容器重启策略和Pod终止过程
  2. Java基础-类型转换、变量、变量命名规范
  3. js检测数组是否有重复的数据,
  4. 刘蓉年谱.PDF
  5. 软件工程实践总结&amp;个人技术博客
  6. spark structured streaming (结构化流) join 操作( 官方文档翻译)
  7. 最简spring IOC实现
  8. brew mongodb 安装 使用 brew update
  9. 查看oracle死锁
  10. python性能测试工具locust