zoukankan      html  css  js  c++  java
  • 【python----发轫之始】【猜数字小游戏】

    import random
    import os.path
    from datetime import datetime
    
    
    def guide_page(guide_word):
        print("*"*30, guide_word, "*"*30)
    
    
    def all_num(n):
        while True:
            if n.isdigit():
                return int(n)
            n = input("您输入的不是数字,请重新输入:")
    
    
    def check_num_legal(n, a, b):
        if a <= n <= b:
            return True
        return False
    
    
    if __name__ == '__main__':
        guide_page("欢迎进入数字猜猜猜小游戏")
    
        begin = all_num(input("请输入数字起始值:"))
        end = all_num(input("请输入数字终止值:"))
        if begin == end:
            print("您输入的数字相同!!请重新启动程序。")
        else:
            v = random.randint(begin, end)
            times = 0
            base_path = os.path.dirname(os.path.abspath(__file__))
            record_path = os.path.join(base_path, 'record')
            with open(record_path, 'w', encoding='utf-8') as f:
                s = all_num(input("请输入您猜测的数字:"))
                f.write("{0} 您第1次猜测的值为:{1}
    ".format(str(datetime.now()), s))
                while True:
                    times += 1
                    if check_num_legal(s, begin, end):
                        if s < v:
                            print("Lower than the answer")
                        elif s > v:
                            print("Higher than the answer")
                        else:
                            print("恭喜您!只用了{0}次就赢得了游戏".format(times))
                            f.write("恭喜您!只用了{0}次就赢得了游戏".format(times))
                            break
                    else:
                        print("您输入的数字不在指定区间,请您输入合法数字。")
                    print("*"*50)
                    s = all_num(input("请继续输入您猜测的数字:"))
                    f.write("{0} 您第{2}次猜测的值为:{1}
    ".format(str(datetime.now()), s, times+1))
  • 相关阅读:
    Sqlserver日期函数应用
    SSRS匿名访问
    SSAS动态添加分区(一)
    BI就是报表?
    CreateEvent函数/多线程/c++
    字符编码介绍
    Win7 64下Visual C++ 6.0不兼容
    Winpcap安装,Cannot open include file 'pcap.h'
    PPT开发 * .pps 文件类型
    Visual Assist X 工具栏不显示 toolbar
  • 原文地址:https://www.cnblogs.com/Attacking-vincent/p/12969042.html
Copyright © 2011-2022 走看看