博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
01-python3.5-模块导入-while-for-range-break-continue
阅读量:6848 次
发布时间:2019-06-26

本文共 3313 字,大约阅读时间需要 11 分钟。

一、输入用户名和密码----导入getpass模块

1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 #Author:XZ 4  5 """ 6 name = input("What's your name ?") 7 print("Hello! " +  name) 8  9 """10 #########11 #输入密码时,如果想要不可见,需要利用getpass 模块中的 getpass方法,即:12 import getpass13 14 _username = 'alex'15 _password = 'abc123'16 username = input( "username:")17 # 将用户输入的内容赋值给 password 变量18 password = getpass.getpass("password:")19 20 #getpass在pycharm中不太好用,建议使用window命令行测试如:21 22 # E:\Users\admin>python I:\pycharm\PycharmProjects\20180908PY\20190908\1-day1-01.py23 # username:alex24 # password:25 # alex aaaa26 # E:\Users\admin>27 28 if _username == username and _password == password:29     print("Welcome user {name} login...".format(name=username))30 else:31     print("Invalid username or password !")32 33 print(username,password)

 

二、通过while循环猜年龄

1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 #Author:XZ 4  5 age_of_oldboy = 56 6  7 count = 0 8  9 while count < 3:10     guess_age = int(input("guess age:"))11 12     if guess_age == age_of_oldboy:13         print("yes, you get it.")14         break15     elif guess_age > age_of_oldboy:16         print("Think smaller!")17     else:18         print("Think bigger!")19     count +=120 else:21     print("you have tried too many times.. fuck off")

 

三、while循环打印到1亿的数字

1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 #Author:XZ 4  5 count = 0 6  7 while True: 8     print("count:",count) 9     count = count + 1   #count +=110     if count == 100000000:11         break

 

四、range循环打印0-9的数字

1 #!/usr/bin/env python2 # -*- coding:utf-8 -*-3 #Author:XZ4 5 for i in range(10):6     print("loop",i)

 

五、通过range设置猜年龄游戏3次猜错强制退出

1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 #Author:XZ 4  5 age_of_oldboy = 56 6  7  8 for i in range(3): 9     guess_age = int(input("guess age:"))10 11     if guess_age == age_of_oldboy:12         print("yes, you get it.")13         break14     elif guess_age > age_of_oldboy:15         print("Think smaller!")16     else:17         print("Think bigger!")18 else:19     print("you have tried too many times.. fuck off")

 

1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 #Author:XZ 4  5 age_of_oldboy = 56 6  7 count = 0 8  9 while count < 3:10     guess_age = int(input("guess age:"))11 12     if guess_age == age_of_oldboy:13         print("yes, you get it.")14         break15     elif guess_age > age_of_oldboy:16         print("Think smaller!")17     else:18         print("Think bigger!")19     count +=120     if count == 3:21         countine_confirm = input("Do you want to keep guessing ?")22         if countine_confirm != "n":23             count = 024 else:25     print("you have tried too many times.. fuck off")

 

 

 

六、通过range循环打印10以内的偶数

1 #!/usr/bin/env python2 # -*- coding:utf-8 -*-3 #Author:XZ4 for i in range(0,11,2):5     print("loop",i)

 

七、循环中break和continue的使用

1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 #Author:XZ 4  5 for i in range(1,10): 6     if i < 3: 7         print("loop",i) 8     else: 9         continue10     print("hehe...")11 #continue 跳出本次循环,进入下一次循环12 #break 结束整个循环

 

1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 #Author:XZ 4  5 for i in range(10): 6     print('------------',i) 7     for j in range(10): 8         print(j) 9         if j > 5:10             break

 

转载于:https://www.cnblogs.com/mypython2018/p/9655263.html

你可能感兴趣的文章
until循环语句
查看>>
Android桌面悬浮窗进阶,QQ手机管家小火箭效果实现
查看>>
提高用户体验方式:饥饿营销
查看>>
Java8中的LocalDateTime工具类
查看>>
Exchange 2013 PowerShell创建自定义对象
查看>>
RAID-10 阵列的创建(软)
查看>>
javaScript的调试(四)
查看>>
nginx不使用正则表达式匹配
查看>>
dell台式机双SATA硬盘开机提示NO boot device available- Strike F1 to retryboot .F2
查看>>
linux下mysql的卸载、安装全过程
查看>>
samba不需密碼的分享
查看>>
利用putty进行vnc + ssh tunneling登录
查看>>
js重定向---实现页面跳转的几种方式
查看>>
hadoop1.x作业提交过程分析(源码分析第二篇)
查看>>
默认安装vsftpd后
查看>>
极速理解设计模式系列:14.轻量级模式(Flyweight Pattern)
查看>>
深度有趣 | 12 一起来动动手
查看>>
相关算法排序安排
查看>>
css的bug:
查看>>
《Redis设计与实现》读书笔记
查看>>