- 写循环猜年龄程序,猜错三次则打印提示信息并退出循环,猜对也打印提示信息并退出循环
count=0 while count < 3: num = input("猜年龄游戏:") guess = int(num) if guess == 18: print("恭喜你猜对了") elif guess > 18: print("大了") elif guess < 18: print("小了") count += 1 else: print("猜了三次都没猜对‘")
- 写while循环嵌套的小例子,用一个tag控制所有while循环的退出
count=0 tag="True" while True: if count >= 3: tag="False" break username = input("请输入用户名:") password = input("请输入密码:") if username == "hello" and password == "123": print("登录成功") while True: if count >= 3: print("猜了三次也没猜中!") break print("猜数字:") guess=int(input("请输入一个数字:")) if guess == 18: print("猜对了") elif guess > 18: print("大了") else: print("小了") count += 1 else: print("用户名不存在或密码错误!") count +=1