if、for、while语句
作者:互联网
表达式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 i 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 i in range ( 10 ):
if i< 5 :
continue #不往下走了,直接进入下一次loop
print ( "loop:" , i )
|
需求二:还是上面的程序,但是遇到大于5的循环次数就不走了,直接退出
1 2 3 4 |
for i 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