python 循环语句while

"""

while 条件语句:
    满足条件执行的语句
else:
    不满足条件执行的语句

"""

# 1. 1+2+3+.....+100

# i+=1  i=i+1
sum = 0
i = 1
while  i <=100:
    sum += i
    i += 1
print(sum)

# 2. 把用户登陆的代码for循环部分改写为while循环;

trycount = 0
while trycount < 3:


else:
    print("登陆超过三次")

###################################while循环练习_猜数字游戏################################

"""
# 猜数字游戏                                                                                               
     if , while, break
     1. 系统随机生成一个1~100的数字;
     ** 如何随机生成整型数, 导入模块random, 执行random.randint(1,100);
     2. 用户总共有5次猜数字的机会;
     3. 如果用户猜测的数字大于系统给出的数字,打印“too big”;
     4. 如果用户猜测的数字小于系统给出的数字,打印"too small";
     5. 如果用户猜测的数字等于系统给出的数字,打印"恭喜中奖100万",并且退出循环;

# 方法1:
import random

num = random.randint(1,100)

print(num)
for i in range(1,6):
        guess = int(input("plese guess:"))
        if guess > num :
                print("too big")
        elif guess < num :
                print("too small")
        else :
                print("congratulation!!!")
                exit()
else:
        print("game over")
"""

# 方法2:


import random
x = random.randint(1,100)
print(x)
trycount = 0
while trycount < 5:
        tk = int(input("你猜:"))
        if tk == x:
                print("恭喜中奖100万")
                break
        elif tk < x:
                print("too small")
                trycount += 1
        else:
                print("too big")
                trycount += 1
else:
        print("没有机会")

###################################while死循环##########################

"""
while True:
    pass

while 1:  # bool(1)
    pass
while 2>1:
    pass
"""

猜你喜欢

转载自blog.csdn.net/zcx1203/article/details/81588082