zoukankan      html  css  js  c++  java
  • Day1_Python基础_15.while loop

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

    海枯石烂代码

    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= 10
    count = 0
    while 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
     
  • 相关阅读:
    Java 零基础跑起第一个程序
    Xcode6 引入第三方静态库project的方法
    Cocos2d-x3.0 RenderTexture(三)
    POJ 题目3461 Oulipo(KMP)
    unity3D游戏开发实战原创视频讲座系列9之塔防类游戏开发第一季
    android 虚拟按键是通过哪种机制上报的?
    深入理解 C 指针阅读笔记 -- 第五章
    【1】按照Django官网,编写一个web app 创建project/配置数据库
    [Swift]LeetCode442. 数组中重复的数据 | Find All Duplicates in an Array
    [Swift]LeetCode441. 排列硬币 | Arranging Coins
  • 原文地址:https://www.cnblogs.com/wangcx/p/6701043.html
Copyright © 2011-2022 走看看