zoukankan      html  css  js  c++  java
  • Python--循环--for && while

    for循环示例:猜数字游戏

     1 winning_number = 38
     2 for i in range(3):
     3     guess_num = int(input("guess num:") )
     4     if guess_num == winning_number :
     5         print("yes, you got it. ")
     6         break
     7     elif guess_num < winning_number:
     8         print("think smaller...")
     9     else:
    10         print("think bigger!")
    11 else:
    12     print("you have tried too many times..")

    while循环示例:死循环

    1 count = 0
    2 while True:
    3     print("I love You!!!---%d" %(count))
    4     count += 1
    5     if count > 999:
    6         break

    while循环示例:猜数字游戏

     1 winning_number = 38
     2 count = 0
     3 while count <3:
     4     guess_num = int(input("guess num:") )
     5     if guess_num == winning_number :
     6         print("yes, you got it. ")
     7         break
     8     elif guess_num < winning_number:
     9         print("think smaller...")
    10     else:
    11         print("think bigger!")
    12     count +=1
    13 else:
    14    print("you have tried too many times..")

    while循环升级:根据用户输入来决定是否终止游戏

     1 winning_number = 38
     2 count = 0
     3 while count <3:
     4     guess_num = int(input("guess num:") )
     5     if guess_num == winning_number :
     6         print("yes, you got it. ")
     7         break
     8     elif guess_num < winning_number:
     9         print("think smaller...")
    10     else:
    11         print("think bigger!")
    12     count +=1
    13     if count == 3:
    14         countine_confirm = input("do you want to keep guessing..?")
    15         if countine_confirm != 'n':
    16             count =0
  • 相关阅读:
    JSP/Servlet相关
    mysql 相关问题解决
    Git常用
    利用JDBC连接MySQL并使用MySQL
    memcache、redis原理对比
    Python 2.7.x 和 3.x 版本的重要区别
    python 单例模式
    python 装饰器原理及用法
    python 冒泡排序
    python 迭代器和生成器
  • 原文地址:https://www.cnblogs.com/sunnytomorrow/p/12846257.html
Copyright © 2011-2022 走看看