【入门小程序:Python实现猜数字小游戏】

由于学Python也有一段时间了。想试着写个猜游戏的小程序;
效果图如下:
在这里插入图片描述
实现的代码如下:

import random     #随机生出数模块
import sys        ##用到的方法sys.exit
import prettytable    ##快速生成表格
from datetime import datetime    ##time库的扩展库。主要用来生成当前时间

name = str(input("请输入你的姓名:"))    
s = random.randint(0,10)  ##生成1-10以内的随机数
count = 0
first_guess = ""
second_guess = ""
third_guess = ""
the_prize = ""
print ("你一共有3次机会.")
while True:
    x = prettytable.PrettyTable(["时间","名字","第一次","第二次","第三次","是否猜对"])   ##表格列名
    while count <3:
        count+=1
        r = int(input("\n请输入你的数:"))
        if r>s:
            print ("你输入的大于中奖数,请继续")
            the_prize = "no"
        elif r<s:
            print ("你输入的数小于中奖数,请继续")
            the_prize = "no"
        else:
            print ("恭喜你猜对了")
            the_prize = "yes"
            if count == 1:
              first_guess = r
            elif count == 2:
              second_guess = r
            else:
              third_guess = r
            break
        if count == 1:
            print ("你还有俩次机会")
            first_guess = r
        elif count == 2:
            print ("你还有一次机会,请谨慎")
            second_guess = r
        else:
            print ("你没有机会了,请再接再厉,即将退出本次游戏")
            third_guess = r
    ##把值添加到表格中
    x.add_row([datetime.now().strftime("%Y-%m-%d %H:%M:%S"),name,first_guess,second_guess,third_guess,the_prize])
    print (x)
    ##判断用户是否还想继续玩
    e = str(input("输入'q'退出游戏,输入任意键继续游戏"))
    if e == 'q':
        print ("即将退出游戏")
        sys.exit(0)
    else:
      count = 0
  

写的不好,请各位纠正。

  • 5
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值