声明变量
#!/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7 # -*- coding:utf-8 -*- # caidongsheng # 2019-09-08 21:21 # example20.py # PyCharm name = "caidongsheng"
变量定义的规则
变量的赋值
#!/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7 # -*- coding:utf-8 -*- # caidongsheng # 2019-09-08 21:21 # example20.py # PyCharm name = "caidongsheng" name2 = name print(name,name2) ## caidongsheng caidongsheng name = "lixue" print("name2 is ",name2) ##name2 is caidongsheng
#!/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7 # -*- coding:utf-8 -*- # caidongsheng # 2019-09-08 21:21 # example20.py # PyCharm # 提示输入用户名和密码 # 验证用户名和密码 # 如果错误,则输出用户名或密码错误 # 如果成功,则输出 欢迎,XXX! import getpass name = input(‘请输入用户名:‘) ##input输入获取的值都为字符串!!! pwd = getpass.getpass(‘请输入密码:‘) if name == "caidongsheng" and pwd == "cmd": print("欢迎,caidongsheng") else: print("用户名和密码错误")
语法规则
while 条件: # 循环体 # 如果条件为真,那么循环体则执行 # 如果条件为假,那么循环体不执行
break
break用于退出所有循环
#!/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7 # -*- coding:utf-8 -*- # caidongsheng # 2019-09-08 21:37 # example21.py # PyCharm while True: print("123") break print("456") 输出结果: 123
continue
continue用于退出当前循环,继续下一次循环
#!/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7 # -*- coding:utf-8 -*- # caidongsheng # 2019-09-08 21:37 # example21.py # PyCharm while True: print("123") continue print("456")
输出结果: 123 123 ...
1、使用while循环输入 1 2 3 4 5 6 8 9 10
#!/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7 # -*- coding:utf-8 -*- # caidongsheng # 2019-09-08 10:07 # example19.py # PyCharm num = 1 while num <= 10: if num == 7: pass else: print(num) num += 1
2、求1-100的所有数的和
#!/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7 # -*- coding:utf-8 -*- # caidongsheng # 2019-09-08 10:07 # example19.py # PyCharm sum = 0 i = 1 while i <= 100: sum = sum + i i = i + 1 print(sum)
3、输出 1-100 内的所有奇数
#!/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7 # -*- coding:utf-8 -*- # caidongsheng # 2019-09-08 10:07 # example19.py # PyCharm sum01 = 0 j = 1 while j <= 100: if j % 2 == 1: sum01 = sum01 + j else: pass j = j + 1 print("1到100,奇数和是: ",sum01)
4、输出 1-100 内的所有偶数
#!/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7 # -*- coding:utf-8 -*- # caidongsheng # 2019-09-08 10:07 # example19.py # PyCharm sum02 = 0 j1 = 1 while j1 <= 100: if j1 % 2 == 0: sum02 = sum02 + j1 else: pass j1 = j1 + 1 print("1到100,偶数和是: ",sum02)
5、求1-2+3-4+5 ... 99的所有数的和
#!/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7 # -*- coding:utf-8 -*- # caidongsheng # 2019-09-08 10:07 # example19.py # PyCharm sum03 = 0 sum04 = 0 j2 = 1 while j2 <= 99: if j2 % 2 == 0: sum03 = sum03 - j2 else: sum04 = sum04 + j2 j2 = j2 + 1 print("和是: ",sum04+sum03)
6、用户登陆(三次机会重试)
#!/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7 # -*- coding:utf-8 -*- # caidongsheng # 2019-09-08 10:07 # example19.py # PyCharm
user_name = input("请输入用户名:")
loop = 1
while loop <= 3:
user_name = input("请输入用户名:")
user_pass = input("请输入密码: ")
if user_name == "caidongsheng" and user_pass =="123456":
print("欢迎进入!!")
break
else:
print("用户或者密码错误!!")
loop = loop + 1
原文:https://www.cnblogs.com/caidongsheng94/p/11488745.html