标签:rand ESS imp 重复 user random break 登录 family
#循环、遍历、迭代
# for循环、 while循环
看数据类型
type(name) int
break 不管循环有没有完成,立即结束循环
continue 结束本次循环,继续进行下一次循环
一、while循环
# while循环,必须有一个计数器
# 循环就是在重复的执行循环体里面的代码
count = 0
while count<10:
print(‘你好‘)
count = count+1
eg:
# 猜数字,如果猜大了,提示太大,猜小了,就提示小,猜对了就结束
import random
num = random.randint(1,100)# 产生一个随机的数字
count = 0
while count<7:
guess = input(‘请输入你猜的数字:‘)
guess = int(guess)
if guess>num:
print(‘猜大了‘)
continue
elif guess<num:
print(‘猜小了‘)
continue
else:
print(‘恭喜你猜对了‘)
break #立即结束循环
count = count+1
# if count==7:
else:
print(‘次数用完了‘)
二、for循环
import random
num = random.randint(1,100)
for i in range(3):
guess = input(‘请输入你猜的数字:‘)
guess = int(guess)
if guess > num:
print(‘猜大了‘)
continue
elif guess < num:
print(‘猜小了‘)
continue
else:
print(‘恭喜你猜对了‘)
break # 立即结束循环
else:
print(‘游戏次数用完了‘)
for循环之上课练习-登录,代码如下:
# 错误次数3次
for i in range(3):
username = input(‘请输入你的用户名:‘)
pwd = input(‘请输入你的密码:‘)
if username == ‘nhy‘ and pwd == ‘123456‘:
print(‘欢迎光临,登录成功!‘)
break
else:
print(‘账号/密码错误!‘)
continue
else:
print(‘错误次数已经用光了,请明天再试!‘)
标签:rand ESS imp 重复 user random break 登录 family
原文地址:https://www.cnblogs.com/Noul/p/9126217.html