python loop循环_python -- 流程判断 & 循环loop

python -- 流程判断 & 循环loop

流程判断 if-elif-else 那些事

if-else

name,passwd = 'Gao',"abc123"

user_name = input("User Name is : \n")

user_password = input("User Password is : \n")

if name == user_name and passwd == user_password:

print("Welcome {0} login...".format(user_name))

else:

print("Invail username or password...")

if-elif-else

old_boy_age = "40"

guess_age = input("Guess age is : \n")

if guess_age == old_boy_age:

print("Bingo")

elif guess_age > old_boy_age:

print("Higher! Guess lower")

else:

print("Lower! Guess higher")

循环loop

while

一个简单的while,死循环。

count = 0

while True:

count += 1

print(count)

带break的while

count = 0

while True:

print("Count:", count)

count += 1

if count > 100:

break

不带计数器的while

old_boy_age = "40"

while True:

guess_age = input("Guess age is : \n")

if guess_age == old_boy_age:

print("Bingo")

break

elif guess_age > old_boy_age:

print("Higher! Guess lower")

else:

print("Lower! Guess higher")

不带计数器的while,且有多余的代码

old_boy_age = "40"

while True:

guess_age = input("Guess age is : \n")

if guess_age == old_boy_age:

print("Bingo")

break

elif guess_age > old_boy_age:

print("Higher! Guess lower")

else:

# 这句话在pycharm里会提示【Statement seems to have no effect】,说明这句是废话,或没有意义。

guess_age < old_boy_age

print("Lower! Guess higher")

带计数器的while,low版

old_boy_age = "40"

count = 0

while True:

# 此处加入一个输入的次数判断

if count == 3:

break

guess_age = input("Guess age is : \n")

if guess_age == old_boy_age:

print("Bingo")

break

elif guess_age > old_boy_age:

print("Higher! Guess lower")

else:

print("Lower! Guess higher")

# 一般在所有判断最后把计数器加上

count += 1

带计数器的while,high版

old_boy_age = "40"

count = 0

while count < 3: # 此处把上一代码的if判断提前

guess_age = input("Guess age is : \n")

if guess_age == old_boy_age:

print("Bingo")

break

elif guess_age > old_boy_age:

print("Higher! Guess lower")

else:

print("Lower! Guess higher")

# 一般在所有判断最后把计数器加上

count += 1

带else和break的while

old_boy_age = "40"

count = 0

while count < 3: # 此处把上一代码的if判断提前

guess_age = input("Guess age is : \n")

if guess_age == old_boy_age:

print("Bingo")

break

elif guess_age > old_boy_age:

print("Higher! Guess lower")

else:

print("Lower! Guess higher")

# 一般在所有判断最后把计数器加上

count += 1

else:

print("Too many times...")

带计数器的while,high版,人性版

old_boy_age = "40"

count = 0

flag = True

while flag:

guess_age = input("Guess age is : \n")

if guess_age == old_boy_age:

print("Bingo")

break

elif guess_age > old_boy_age:

print("Higher! Guess lower")

else:

print("Lower! Guess higher")

# 一般在所有判断最后把计数器加上

count += 1

if count < 3:

continue

else:

print("Too many times!\n")

choice = input("还要继续吗?按Y继续,N退出\n")

if choice == 'Y':

flag = True

elif choice =="N":

flag = False # todo这里还要把input的大小写调整下。

Alex版

'''

Alex版vs我的版本

A版用了不等于【!=】的概念,把代码简化。

'''

old_boy_age = "40"

count = 0

while count < 3:

guess_age = input("Guess age is : \n")

if guess_age == old_boy_age:

print("Bingo")

break

elif guess_age > old_boy_age:

print("Higher! Guess lower")

else:

print("Lower! Guess higher")

# 一般在所有判断最后把计数器加上

count += 1

if count == 3:

# 此处高亮打印

print("\033[32;1mToo many times!\n\033[0m")

# todo这里还要把input的大小写调整下。

continue_confirm = input("还要继续吗?按任意键继续,按N退出\n")

if continue_confirm != 'N':

count = 0

for

一个简单的for循环

# i这里是一个临时变量

for i in range(1, 10):

print("The loop is :", i)

带步长的for循环

# 步长默认值:1

for i in range(1, 10, 2):

print("The loop is :", i)

for-else

old_boy_age = "40"

# 这里用for循环代替了while count < 3

for i in range(3):

guess_age = input("Guess age is : \n")

if guess_age == old_boy_age:

print("Bingo")

break

elif guess_age > old_boy_age:

print("Higher! Guess lower")

else:

print("Lower! Guess higher")

# 如果这里不加else,就代表for的3次走完后都要打印下面的内容。

#上面for的内容,正常走完后,就会执行这句else的内容。如果break,这句else就不执行了。

else:

print("Too many times...")

for-continue

for i in range(10):

if i < 5:

print("The loop is :",i)

else:

# continue的意思是,遇到continue就不执行循环内,下面的的代码

continue

print("The end")

loop套loop,两层loop

for i in range(1, 6):

print('\033[32;1mThe 1st loop:\033[0m', i)

for j in range(1, 6):

if j > 3:

print(j)

break

else:

# 用了format后,可以将总句话都变高亮

print('\033[32;1mThe 2nd loop:{0}\033[0m'.format(j))

九九乘法表

for i in range(1, 10):

for j in range(1, i+1):

print('{}x{}={}\t'.format(i, j, i*j), end='')

print()

# 通过指定end参数的值,可以取消在末尾输出回车符,实现不换行。

for vs while

for循环只能对一些东西的集合进行循环,while循环可以对任何对象进行驯化,然而,while循环比起来更难弄对,而一般的任务用for循环更容易一些。

写在后面

Indentation=缩进,python强制缩进,所以缩进很重要。IndentationError=缩进错误。

如果一行代码本身就是顶级的话,就需要顶格写,不然会报缩进错。

continue是跳出本次循环,执行下一次循环,即不执行后面的代码。

break是结束本次大的循环。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值