找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 2334|回复: 2

[已解决] 求助int()很奇怪的报错

0

主题

2

帖子

2

积分

贫民

积分
2
MeePwn 发表于 2020-8-22 13:53:21 | 显示全部楼层 |阅读模式
本帖最后由 MeePwn 于 2020-8-23 10:12 编辑

import pygame,sys
from pygame.locals import *
class Trivia:
    def __init__(self,filename):
        self.data = []
        self.current = 0
        self.total = 0
        self.correct = 0
        self.score = 0
        self.scored = False
        self.failed = False
        self.wronganswer = 0
        self.colors = [white,white,white,white]

        file = open(filename,'r')
        trivia_data = file.readlines()
        file.close()

        for text_data in trivia_data:
            self.data.append(text_data.split())
            self.total += 1
    def show_question(self):
        print_text(font1,210,5,'TRIVIA GAME')
        print_text(font2,190,500-20,'Press Keys(1-4) To Answer',purple)
        print_text(font2,530,5,'SCORE',purple)
        print_text(font2,550,25,str(self.score),purple)

        # get correct answer out of data (first)
        self.correct = int(self.data[self.current + 5])

        # dis the question
        question = self.current//6 +1
        print_text(font1,5,80,'QUESTION'+str(question))
        print_text(font2,20,120,self.data[self.current],yellow)

        # respond to correct answer
        if self.scored:
            self.colors = [white,white,white,white]
            self.colors[self.correct-1] = green
            print_text(font1,230,380,'CORRECT!',green)
            print_text(font2,170,420,'Press Enter For Next Question',green)
        elif self.failed:
            self.color = [white,white,white,white]
            self.colors[self.wronganswer-1] = red
            self.colors[self.correct-1] = green
            print_text(font1,220,380,'INCORRECT!',red)
            print_text(font2,170,420,'Press Enter For Next Question',red)
        # display the answer
        print_text(font1,5,170,'ANSWERS')
        print_text(font2,20,210,'1-'+self.data[self.current+1],self.colors[0])
        print_text(font2,20,240,'2-'+self.data[self.current+2],self.colors[1])
        print_text(font2,20,270,'3-'+self.data[self.current+3],self.colors[2])
        print_text(font2,20,300,'4-'+self.data[self.current+4],self.colors[3])

        def handle_input(self,number):
            if not self.scored and not self.failed:
                if number == self.correct:
                    self.scored = True
                    self.score += 1
                else:
                    self.failed = True
                    self.wronganswer = number
  File "c:/Users/Administrator/Desktop/Trivia Game.py", line 37, in show_question    self.correct = int(self.data[self.current + 5])TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'


超过10000字节(帖子限值),我把源代码发到附件了,麻烦大神帮我看下,实在搞不懂为什么报错是 列表了



回复

使用道具 举报

0

主题

956

帖子

956

积分

圣骑士

积分
956
sheeboard 发表于 2020-8-24 10:48:00 | 显示全部楼层
data是list,int必须是string,如果想int一个list可以用map(int,a)
回复 支持 反对

使用道具 举报

0

主题

2

帖子

2

积分

贫民

积分
2
MeePwn  楼主| 发表于 2020-8-29 09:06:36 | 显示全部楼层
sheeboard 发表于 2020-8-24 10:48
data是list,int必须是string,如果想int一个list可以用map(int,a)

谢谢,我已经发现了问题是出在 split上,返回了一个列表
如何用split来分割呢(保留空格,只去掉\n呢)
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表