猫史档案馆


【Python作品分享】二进制游戏

用户:wrmdcxywrmdcxy查看:12 回复:3 评论:12 创建时间:2020-01-21T11:09:42


【作品展示】

center_image

 

【作品介绍】

这是一款二进制游戏,作者花了三天做完。两天编程,一天改进。希望大家多多支持!

游戏规则:运行即可。

编程语言:Python

闪光点:分数可以保存。

 

【作品源代码】

import time
from random import randint
from os import rename,remove
from 二进制和十进制的转换 import Base2ChangetoBase10

UserName=input('你好,请输入您的名字:')
inputs=open('用户分数.txt','r')
lll='j'
while lll!='':
    lll=inputs.readline()
    content=lll.split(',')
    if content[0]==UserName:
        UserScores=content[1]
        UserScores=int(UserScores)
        inputs.close()
        break
try:
    UserScores+=1
    UserScores-=1
except NameError:
    inputs.close()
    UserScores='-1'
if UserScores=='-1':
    newu='True'
    UserScores=0
    print('欢迎加入这个游戏!')
    time.sleep(0.5)
    print('''二进制游戏规则:
    在这个游戏中,你会得到一个十进制的数。
    你的任务是将这个数转换为二进制的数。
    答对一题加一分,答错一题不扣分。''')
    time.sleep(1)
    print('现在开始吧')
else:
    newu='False'
    print('你好,%s,欢迎回来!您当前的分数是:%d'%(UserName,UserScores))
    while True:
        aaa=input('需要再讲一次游戏规则吗?(1.要  2.不要):')
        if aaa=='1':
            print('好的')
            time.sleep(0.5)
            print('''二进制游戏规则:
            在这个游戏中,你会得到一个十进制的数。
            你的任务是将这个数转换为二进制的数。
            答对一题加一分,答错一题不扣分。''')
            break
        elif aaa=='2':
            print('好的,现在开始吧')
            break
        else:
            print('请输入数字选项')
choise=1
while choise!='-1':
    time.sleep(0.5)
    Question=int(input('问题数(1~15):'))
    if Question<1:
        print('\n问题数不能小于1。\n因此,问题数将设置为1。')
        Question=1
    if Question>15:
        print('\n问题数不能大于15。\n因此,问题数将设置为15。')
        Question=15
    _Question=Question
    for __count in range(Question):
        QuestionNumber=str(randint(2,30))
        answer=input(QuestionNumber+'=')
        while True:
            try:
                answer=int(answer)
                break
            except:
                print('请输入正确的数字。')
                answer = int(input(QuestionNumber, '='))
        b10answer = Base2ChangetoBase10(answer)
        if b10answer==int(QuestionNumber):
            print('答案正确!')
            UserScores+=1
        else:
            print('答案错误。')
            _Question-=1
    print('游戏结束,您答了%d题,答对了%d题。'%(Question,_Question),end='')
    choise=input('还想玩吗?继续按Enter键,不想玩可以输入“-1”退出哦')
print('您当前的分数是%d分。拜拜,欢迎下次来玩!'%(UserScores))
if newu=='True':
    tinput = open('用户分数.txt','a')
    tinput.write(UserName+','+str(UserScores)+'\n')
    tinput.close()
else:
    temp=open('newus.txt','w')
    tinput=open('用户分数.txt','r')
    for lline in tinput:
        content=lline.split(',')
        if content[0]==UserName:
            temp.write(UserName+','+str(UserScores)+'\n')
        else:
            temp.write(lline)
    tinput.close()
    temp.close()
    remove('用户分数.txt')
    rename('newus.txt', '用户分数.txt')

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
dounsm36dounsm36

“二进制与十进制的转换”呢?

点赞0


评论


wrmdcxywrmdcxy

出现FireNotFoundError的同学,请创建一个文本文档(.txt)并把之放到与主程序相同的文件夹中即可

出现ModuleNotFoundError的同学,请创建一个新的python文件,文件名更改为“二进制与十进制的转换.py”,然后前往https://shequ.codemao.cn/community/236636把代码搬下来放到“二进制与十进制的转换.py”中,并把之放到与主程序相同的文件夹中即可

点赞0


评论


wrmdcxywrmdcxy

噢,我的记性可真差!我恨不得把这篇帖子删了!

要问我为什么?我为了二进制的转换,去做了一个模块。

可是,我突然想起,Python是有这个函数的!!!

bin()转二进制                                     oct()转八进制

hex()转十六进制                                  int()转十进制

哎……

我已做了更新,URL:https://shequ.codemao.cn/community/238429

点赞0


评论