Day1_Python基础_15.while loop

流过昼夜 提交于 2019-12-16 20:23:34

有一种循环叫死循环,一经触发,就运行个天荒地老、海枯石烂。

海枯石烂代码

count = 0
while True:
    print("你是风儿我是沙,缠缠绵绵到天涯...",count)
    count +=1

其实除了时间,没有什么是永恒的,死loop还是少写为好 

上面的代码循环100次就退出吧

count = 0
while True:
    print("你是风儿我是沙,缠缠绵绵到天涯...",count)
    count +=1
    if count == 100:
        print("去你妈的风和沙,你们这些脱了裤子是人,穿上裤子是鬼的臭男人..")
        break

回到上面for 循环的例子,如何实现让用户不断的猜年龄,但只给最多3次机会,再猜不对就退出程序

#!/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("猜这么多次都不对,你个笨蛋.")

# Author: George.Wang

'''
i=0
while True:
    print("i=",i)
    i=i+1   #=i +=1
    if i>10:
        break
'''

'''
my_age_count = 0
my_age = 10
while True:
    if my_age_count ==3:
        break
    guess_age = int(input("You can guess my age:"))
    if guess_age == my_age:
        print("Congratulations! You got my age.")
        break
    elif guess_age > my_age:
        print("your answer is bigger than that.let's try again.")
    else:
        print("your answer is smaller than that.let's try again.")
    my_age_count = my_age_count +1   #my_age_count +=1
'''

#------------------------代码优化后+after typing three times it will show "else..."------------------------
my_age_count = 0
my_age = 10
while my_age_count <3:
    guess_age = int(input("You can guess my age:"))
    if guess_age == my_age:
        print("Congratulations! You got my age.")
        break
    elif guess_age > my_age:
        print("your answer is bigger than that.let's try again.")
    else:
        print("your answer is smaller than that.let's try again.")
    my_age_count = my_age_count +1   #my_age_count +=1
else:
    print("You have tried too many times...get the fuck off")
#---------------------判断错3次后退出前,问是否继续--------------------------
my_age= 10count = 0while count <3:    guess_age = int(input("Please input my age:"))    if guess_age == my_age:        print("Congratulations! you got my age.")        break    elif guess_age > my_age:        print("I'm sorry, your answer is bigger than my age.let's try again.")    else:        print("I'm sorry, your answer is smaller than my age.let's try again.")    count = count +1    if count ==3:        move_on=input("Do you want to keep guessing?[yes/no]")        if move_on != "no" and move_on != "n":            count = 0
 

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!