zoukankan      html  css  js  c++  java
  • Python 猜数字游戏

    分别用while循环和for循环来玩转猜数字游戏

    1.对于限定猜数字次数

    (1)while循环 

    number = 56
    count = 0
    while count < 3:
      guess_number = int(input("guess number:"))
      if guess_number == number:
        print("Yes,you got it.")
        break                              #break 跳出当前循环
      elif guess_number > number:
        print("think smaller...")
      else:
        print("think bigger!")
      count +=1
    else:
      print("you have tried too many times..fuck off")

    (2)for 循环 

    number = 56
    for i in range(3):
      guess_number = int(input("guess number:"))
      if guess_number == number:
        print("Yes,you got it.")
        break
      elif guess_number > number:
        print("think smaller...")
      else:
        print("think bigger!")
    else:
      print("you have tried too many times..fuck off")

    2. 任性玩 ,不限制次数

    number = 56
    count = 0
    while count < 3:
      guess_number = int(input("guess number:"))
      if guess_number == number:
        print("Yes,you got it.")
        break
      elif guess_number > number:
        print("think smaller...")
      else:
        print("think bigger!")
      count +=1

      if count == 3:
       countine_confirm = input("do you want to keep guessing")     #如果猜错3次,提示要不要退出游戏 ,输入n表示退出
       if countine_confirm !='n':
        count = 0

  • 相关阅读:
    二叉排序树
    C# 大端与小端
    【转】C#socket通信
    【转】Github 搜索技巧,快速找到好资源
    web api 跨域请求,ajax跨域调用webapi
    【转】Linux简介及最常用命令
    【转】带你吃透RTMP
    09-vuex基本应用之计数demo
    08-配置vue路由的步骤
    02-原型与原型链
  • 原文地址:https://www.cnblogs.com/Lazycat1206/p/10226433.html
Copyright © 2011-2022 走看看