zoukankan      html  css  js  c++  java
  • python中猜数字小游戏

    1、原始游戏   (input 内建函数用于接收用户输入)

    temp = input("please input an integer:")
    guess = int(temp)
    if guess == 8:
        print("you are right!")
        print("no gift evec though you guess right!")
    else:
        print("wrong!")
        print("right number is 8!")
    print("gave ove!")

     

    2、改进小游戏

    给出输入偏离8时的提示:

    不限制用户执行程序的次数,直到猜中。

    temp = input("please input the number:")
    guess = int(temp)
    
    while guess != 8:
        print("wrong!")
        if guess > 8:
            print("big!")
        else:
            print("small!")
        temp = input("try again:")
        guess = int(temp)
    print("you are right!")
    print("game over!")

    3、改进小游戏

    引入随机答案。

    temp = input("please input an number:")
    guess = int(temp)
    import random
    secret = random.randint(1,10)
    
    while guess != secret:
        print("wrong!")
        if guess > secret:
            print("big!")
        else:
            print("small!")
    
        temp = input("try again:")
        guess = int(temp)
    
    print("you are right!")
    print("game over!")

    4、改进小游戏

    只给三次机会。

    方法1:

    temp = input("please input an number:")
    guess = int(temp)
    
    import random
    secret = random.randint(1,10)
    
    times = 0
    
    while times < 3:
        if guess == secret:
            print("you are right!")
            break
        else:
            print("you are wrong!")
        if guess != secret:
            if guess > secret:
                print("big")
            else:
                print("small")
        times += 1
        if times < 3:
            temp = input("try again:")
            guess = int(temp)
    if times == 3:
        print("times over!")

    方法2:

    temp = input("please input an number:")
    guess = int(temp)
    
    import random
    secret = random.randint(1,10)
    
    times = 1
    
    while guess != secret and times < 3:
        if guess > secret:
            print("big")
        else:
            print("small")
        temp = input("please try again:")
        guess = int(temp)
        times += 1
    
    if times == 3 and guess != secret:
        print("time out!,game over")
    else:
        print("right")
  • 相关阅读:
    OpenCV 机器学习之 支持向量机的使用方法实例
    Lua中调用C函数(lua-5.2.3)
    我的Hook学习笔记
    几种开源分词工具的比較
    利用JasperReport+iReport进行Web报表开发
    移动前端开发者必知必会:移动设备概述
    图表插件--jqplot交互演示样例
    算法之二叉树各种遍历
    repo的小结
    sqlite3经常使用命令&amp;语法
  • 原文地址:https://www.cnblogs.com/liujiaxin2018/p/14714290.html
Copyright © 2011-2022 走看看