猫史档案馆


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

用户:wrmdcxywrmdcxy查看:4 回复:2 评论:4 创建时间:2020-02-07T20:22:58


【作品展示】

center_image

 

【作品介绍】

还记得之前的二进制游戏吗?(https://shequ.codemao.cn/community/226256)本次发布的就是它的更新版!你可能会问:有什么不同啊?

提示:看看导入模块的那里少了什么

呵呵没错,少了一个自制模块!因为我发现,Python中的内置模块也可以达到我的自制模块的结果,所以我就更了新

好处就是:

1.让文件的存放更加随意、更加便捷

2.让大家的电脑的内存得以释放!

 

【作品源代码】

import time
from random import randint
from os import rename,remove

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)
    while True:
        GameChoise=input('请选择游戏:1.二进制游戏  2.十进制游戏')
        if GameChoise!='1' and GameChoise!='2':
            print('请输入正确的数字选项。\n')
        else:
            break
    if GameChoise=='1':
        print('''二进制游戏规则:
    在这个游戏中,你会得到一个十进制的数。
    你的任务是将这个数转换为二进制的数。
    答对一题加一分,答错一题不扣分。''')
    else:
        print('''十进制游戏规则:
    在这个游戏中,你会得到一个二进制的数。
    你的任务是将这个数转换为十进制的数。
    答对一题加一分,答错一题不扣分。''')
    time.sleep(1)
    print('现在开始吧')
else:
    newu='False'
    print('你好,%s,欢迎回来!您当前的分数是:%d'%(UserName,UserScores))
    while True:
        GameChoise=input('请选择游戏:1.二进制游戏  2.十进制游戏:')
        if GameChoise!='1' and GameChoise!='2':
            print('请输入正确的数字选项。')
        else:
            break
    while True:
        aaa=input('需要再讲一次游戏规则吗?(1.要  2.不要):')
        if aaa=='1':
            print('好的')
            time.sleep(0.5)
            if GameChoise=='1':
                print('''二进制游戏规则:
    在这个游戏中,你会得到一个十进制的数。
    你的任务是将这个数转换为二进制的数。
    答对一题加一分,答错一题不扣分。''')
            else:
                print('''十进制游戏规则:
    在这个游戏中,你会得到一个二进制的数。
    你的任务是将这个数转换为十进制的数。
    答对一题加一分,答错一题不扣分。''')
            break
        elif aaa=='2':
            print('好的,现在开始吧')
            break
        else:
            print('请输入数字选项')
choise=1
while choise!='-1':
    time.sleep(0.5)
    while True:
        Question=input('问题数(1~15):')
        try:
            Question=int(Question)
            break
        except ValueError:
            print('请输入正确的数字\n')
    if Question<1:
        print('\n问题数不能小于1。\n因此,问题数将设置为1。')
        Question=1
    if Question>15:
        print('\n问题数不能大于15。\n因此,问题数将设置为15。')
        Question=15
    _Question=Question
    if GameChoise=='1':
        for __count in range(Question):
                QuestionNumber=str(randint(2,30))
                answer=input(QuestionNumber+'=')
                while True:
                    try:
                        answer=int(answer)
                        break
                    except:
                        print('请输入正确的数字。\n')
                        answer=input(QuestionNumber+'=')
                QuestionNumber=int(QuestionNumber)
                Trueanswer=str(bin(QuestionNumber))
                Trueanswer=int(Trueanswer.replace('0b',''))
                if Trueanswer==answer:
                    print('答案正确!')
                    UserScores+=1
                else:
                    print('答案错误,正确答案是%d'%(Trueanswer))
                    _Question-=1
    else:
        for __count in range(Question):
            Trueanswer2=randint(2,30)
            QuestionNumber2=str(bin(Trueanswer2))
            QuestionNumber2=int(QuestionNumber2.replace('0b',''))
            while True:
                answerb=input('%d='%(QuestionNumber2))
                try:
                    answerb=int(answerb)
                    break
                except:
                    print('请输入正确的数字\n')
            if Trueanswer2==answerb:
                print('答案正确')
                UserScores+=1
            else:
                print('答案错误,正确答案是%d'%(Trueanswer2))
                _Question-=1
    print('游戏结束,您答了%d题,答对了%d题。'%(Question,_Question),end='')
    choise=input('还想玩吗?继续按Enter键,不想玩可以输入“-1”退出哦')
    if choise!='-1':
        while True:
            GameChoise = input('请选择游戏:1.二进制游戏  2.十进制游戏')
            if GameChoise != '1' and GameChoise != '2':
                print('请输入正确的数字选项。\n')
            else:
                break
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下一页
wrmdcxywrmdcxy

Error解决方法:

FireNotFoundError:
创建一个文本文档(.txt),改名为“用户分数”并把之放到与主程序相同的文件夹中即可

 

若还有其它错误,欢迎评论哦

点赞0


评论


dounsm36dounsm36

with open("用户分数.txt","w") as f:

    pass

应该是可以创建一个文件在当前目录下的

点赞0


评论