找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 4832|回复: 7

[已回复] 小白求教,int()参数必须是字符串,类似字节的对象或数...

1

主题

4

帖子

4

积分

贫民

积分
4
。。。 发表于 2017-4-24 19:08:08 | 显示全部楼层 |阅读模式
import sys,pygame
from pygame import *

class Trivia(object):
        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]

                #read trivia data from file
                f = open(filename,"r")
                trivia_data = f.readlines()
                f.close()

                #count and clear up trivia data
                for text_line in trivia_data:
                        self.data.append(text_line.strip)
                        self.total+=1

        def show_question(self):
                print_text(font1,210,5,"TRIVIA GAME")
                print_text(font2,190,500-20,"Press Key (1-4) To Answer",purple)
                print_text(font2,530,6,"SCORE",purple)
                print_text(font2,530,25,str(self.score),purple)

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

                #display question
                question = self.current // 6 + 1
                print_text(font1,5,80,"QUESTION" + str(question))
                print_text(font2,20,150,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.colors = [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 answers
                print_text(font1,5,170,"ANSWER")
                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 = numbe

        def next_question(self):
                if self.scored or self.failed:
                        self.scored = False
                        self.failed = False
                        self.correct = False
                        self.colors = [white,white,white,white]
                        self.current += 6
                        if self.current >= self.total:
                                self.current = 0

def print_text(font, x ,y, text, color=(255,255,255), shadow = True):
        if shadow:
                imgText  =font.render(text, True, (0, 0, 0))
                screen.blit(imgText,(x-2, y-2))

        imgText = font.render(text, True, color)
        screen.blit(imgText, (x, y))

#main program begins
pygame.init()
screen = pygame.display.set_mode((600, 500))
pygame.display.set_caption("The Trivia Game")

font1 = pygame.font.SysFont("arial",40)
font2 = pygame.font.SysFont("arial",24)

white = 255, 255, 0
cyan = 0, 255, 255
yellow = 255, 255, 0
purple = 255, 0, 255
green = 0, 255, 0
red = 255, 0, 0

#load the trivia data file
trivia = Trivia("D:\\python\\Trivia_game\\trivia_data.txt")

#repeating loop
while True:
        for event in pygame.event.get():
                if event.type == QUIT:
                        sys.exit()
                elif event.type == KEYUP:
                        if event.key == pygame.K_ESCAPE:
                                sys.exit()
                        elif event.key == pygame.K_1:
                                trivia.handle_input(1)
                        elif event.key == pygame.K_2:
                                trivia.handle_input(2)
                        elif event.key == pygame.K_3:
                                trivia.handle_input(3)
                        elif event.key == pygame.K_4:
                                trivia.handle_input(4)
                        elif event.key == pygame.K_RETURN:
                                trivia.next_question()

        #clear the screen
        screen.fill((0, 0, 200))

        #display tirvia data
        trivia.show_question()

        #update the display
        pygame.display.update()


Traceback (most recent call last):
  File "D:/python/Trivia_game/trivia_game.py", line 128, in <module>
    trivia.show_question()
  File "D:/python/Trivia_game/trivia_game.py", line 33, 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 'builtin_function_or_method'

报int() argument must be a string, a bytes-like object or a number, not 'builtin_function_or_method'这个问题了,
我尝试过self.correct = int(str(self.data[self.current+5]))这种可是不对,小白求教
回复

使用道具 举报

50

主题

1057

帖子

1108

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1108

活跃会员热心会员最佳导师默默耕耘突出贡献优秀版主荣誉管理论坛元老

blueelwang 发表于 2017-4-25 10:19:57 | 显示全部楼层
首先要确认self.data[self.current+5] 这个得到的值是什么,怀疑这个值有问题,建议打印一下这个值试试
回复 支持 反对

使用道具 举报

1

主题

4

帖子

4

积分

贫民

积分
4
。。。  楼主| 发表于 2017-4-25 18:58:00 | 显示全部楼层
首先感谢你的回答,下面这两个是我打印出来的,您看看,谢谢
print(self.data[self.current+5])
<built-in method strip of str object at 0x000001CC21DE3110>

print(self.current+5)
5
回复 支持 反对

使用道具 举报

1

主题

4

帖子

4

积分

贫民

积分
4
。。。  楼主| 发表于 2017-4-25 19:25:51 | 显示全部楼层
blueelwang 发表于 2017-4-25 10:19
首先要确认self.data[self.current+5] 这个得到的值是什么,怀疑这个值有问题,建议打印一下这个值试试 ...


首先感谢你的回答,下面这两个是我打印出来的,你看看,谢谢
打印(self.data [self.current + 5])
<内置方法条的str对象在0x000001CC21DE3110>

print(self。当前+ 5)
5
回复 支持 反对

使用道具 举报

50

主题

1057

帖子

1108

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1108

活跃会员热心会员最佳导师默默耕耘突出贡献优秀版主荣誉管理论坛元老

blueelwang 发表于 2017-4-26 10:18:35 | 显示全部楼层
。。。 发表于 2017-4-25 19:25
首先感谢你的回答,下面这两个是我打印出来的,你看看,谢谢
打印(self.data [self.current + 5])

int()的参数是一个字符串, 而self.data [self.current + 5]并不是字符串, 这个地方貌似有问题, 建议把self.data也打印一下,一步步的调试,这样应该很容易找到问题所在
回复 支持 反对

使用道具 举报

1

主题

4

帖子

4

积分

贫民

积分
4
。。。  楼主| 发表于 2017-4-27 14:03:52 | 显示全部楼层
blueelwang 发表于 2017-4-26 10:18
int()的参数是一个字符串, 而self.data [self.current + 5]并不是字符串, 这个地方貌似有问题, 建议把 ...

还是没有找到问题,这是一个完整的代码,缺少的只不过是D盘下面第一个小文件,初学python(编程),无从下手,如果可以的话您可不可以亲自运行一下,谢谢,(需要pygame库)谢谢
回复 支持 反对

使用道具 举报

0

主题

2

帖子

2

积分

贫民

积分
2
MeePwn 发表于 2020-8-22 13:30:10 | 显示全部楼层
您好,请问您这个问题解决了吗?
回复 支持 反对

使用道具 举报

2

主题

5

帖子

5

积分

贫民

积分
5
vinewood 发表于 2021-10-5 19:04:15 | 显示全部楼层
。。。 发表于 2017-4-25 18:58
首先感谢你的回答,下面这两个是我打印出来的,您看看,谢谢
print(self.data[self.current+5])
<built-in method strip of str object at 0x000001CC21DE3110>
当然不可以
回复 支持 反对

使用道具 举报

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

本版积分规则

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