ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

if、for、while语句

2022-07-23 18:00:45  阅读:170  来源: 互联网

标签:语句 count guess age while print input loop


表达式if ... else

场景一、用户登陆验证

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 # 提示输入用户名和密码    # 验证用户名和密码 #     如果错误,则输出用户名或密码错误 #     如果成功,则输出 欢迎,XXX!     #!/usr/bin/env python # -*- coding: encoding -*-    import getpass       name = raw_input('请输入用户名:') pwd = getpass.getpass('请输入密码:')    if name == "alex" and pwd == "cmd":     print("欢迎,alex!") else:     print("用户名和密码错误")

场景二、猜年龄游戏

在程序里设定好你的年龄,然后启动程序让用户猜测,用户输入后,根据他的输入提示用户输入的是否正确,如果错误,提示是猜大了还是小了

1 2 3 4 5 6 7 8 9 10 11 12 13 14 #!/usr/bin/env python # -*- coding: utf-8 -*-     my_age = 28   user_input = int(input("input your guess num:"))   if user_input == my_age:     print("Congratulations, you got it !") elif user_input < my_age:     print("Oops,think bigger!") else:     print("think smaller!")
外层变量,可以被内层代码使用 内层变量,不应被外层代码使用

for循环(loop)

 

最简单的循环10次

1 2 3 4 5 6 #_*_coding:utf-8_*_ __author__ = 'Alex Li'     for in range(10):     print("loop:", i )

输出:

1 2 3 4 5 6 7 8 9 10 loop: 0 loop: 1 loop: 2 loop: 3 loop: 4 loop: 5 loop: 6 loop: 7 loop: 8 loop: 9

需求一:还是上面的程序,但是遇到小于5的循环次数就不走了,直接跳入下一次循环

1 2 3 4 for in range(10):     if i<5:         continue #不往下走了,直接进入下一次loop     print("loop:", i )

需求二:还是上面的程序,但是遇到大于5的循环次数就不走了,直接退出

1 2 3 4 for in range(10):     if i>5:         break #不往下走了,直接跳出整个loop     print("loop:", i )

 

while循环(loop)  

 有一种循环叫死循环,一经触发,就运行个天荒地老、海枯石烂。

海枯石烂代码

1 2 3 4 5 count = 0 while True:     print("你是风儿我是沙,缠缠绵绵到天涯...",count)     count +=1     

其实除了时间,没有什么是永恒的,死loop还是少写为好 

上面的代码循环100次就退出吧

1 2 3 4 5 6 7 8 count = 0 while True:     print("你是风儿我是沙,缠缠绵绵到天涯...",count)     count +=1     if count == 100:         print("去你妈的风和沙,你们这些脱了裤子是人,穿上裤子是鬼的臭男人..")         break    

回到上面for 循环的例子,如何实现让用户不断的猜年龄,但只给最多3次机会,再猜不对就退出程序。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #!/usr/bin/env python # -*- coding: utf-8 -*-     my_age = 28   count = 0 while count < 3:     user_input = int(input("input your guess num:"))       if user_input == my_age:         print("Congratulations, you got it !")         break     elif user_input < my_age:         print("Oops,think bigger!")     else:         print("think smaller!")     count += 1 #每次loop 计数器+1 else:     print("猜这么多次都不对,你个笨蛋.")

 

 
break和continue的区别?
for i in range(10):
print('----', i)
for j in range(10):
print(j)
if j > 5:
break # 结束当前内部循环
# continue # 跳出当前循环,继续执行下次循环

 

 条件判断语句:
使用if...else...
_username = 'lsj'
_password = 'abc123'
username = input("username:")
password = input("password:")
print(username, password)

# 验证用户名和密码是否正确,使用if...else
if _username == username and _password == password:
print("Welcome user {name} login...".format(name=username))
else:
print("Invalid username or password!!!")

注意:缩进的错误提示:
IndentationError: unindent does not match any outer indentation level
缩进错误:unindent不匹配任何外部缩进级别

# 猜数字小游戏
# 定义自己的年龄23岁
age_of_lsj = 23
# 定义要猜测的年龄
# guess_age = int(input("guess age:"))

# 使用if判断
# if guess_age == age_of_lsj:
# print("yes,you got it!!!")
# elif guess_age > age_of_lsj:
# print("think smaller!!! ")
# elif guess_age < age_of_lsj:
# print("think bigger!!!")

# 使用while循环可以执行多次操作
# count = 0
# while True: # 当TRUE时候执行下面
# print("count :", count)
# count += 1 # count = count + 1

# 可以无限次数猜测
# while True:
# guess_age = int(input("guess age:"))
# if guess_age == age_of_lsj:
# print("yes,you got it!!!")
# break
# elif guess_age > age_of_lsj:
# print("think bigger!!! ")
# else:
# print("think smaller!!!")

# 限制3次机会猜测
count = 0 # 做一个计数器
# while True:
# if count == 3:
# print("执行结束")
# break
while count < 3:
guess_age = int(input("guess age:"))
if guess_age == age_of_lsj:
print("yes,you got it!!!")
break
elif guess_age > age_of_lsj:
print("think bigger!!! ")
else:
print("think smaller!!!")
count += 1

# if count == 3:
else:
print("you have tried too many times..fuck off")


# 认识for循环
for i in range(0, 10, 2):
print("loop", i)

for循环优化while循环的版本
age_of_lsj = 23
for i in range(10):
guess_age = int(input("guess age:"))
if guess_age == age_of_lsj:
print("yes,you got it!!!")
break
elif guess_age > age_of_lsj:
print("thi1nk bigger!!! ")
else:
print("think smaller!!!")
else:
print("you have tried too many times..fuck off")
 
# 需求:
# 1、每次当输入第三次错误时候,提问是否继续。
# 2、当输入回车或者任意一个键盘时候继续玩游戏。
# 3、当输入n的时候结束游戏

age_of_lsj = 23
count = 0
while count < 3:
guess_age = int(input("guess_age:"))
if guess_age == age_of_lsj:
print("yes,you got it!!!")
break
elif guess_age > age_of_lsj:
print("think bigger!!!")
else:
print("think smaller!!!")
count += 1
if count == 3:
countine_confirm = input("do you want to keep guessing..?")
if countine_confirm != 'n':
count = 0

标签:语句,count,guess,age,while,print,input,loop
来源: https://www.cnblogs.com/liunaixu/p/16512608.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有