Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
394 views
in Technique[技术] by (71.8m points)

python 3.x - TypeError: unsupported operand type(s) for +: 'int' and 'str'

This code is for a trivia game, and every time someone gets a question right, they are awarded with a certain amount of points per question. I do not know how to get the code to add the points together after each right answer. Every time I try something different, I always get this error message.

import sys

def open_file(file_name, mode):
    """Open a file."""
    try:
        the_file = open(file_name, mode)
    except IOError as e:
        print("Unable to open the file", file_name, "Ending program.
", e)
        input("

Press the enter key to exit.")
        sys.exit()
    else:
        return the_file

def next_line(the_file):
    """Return next line from the trivia file, formatted."""
    line = the_file.readline()
    line = line.replace("/", "
")
    return line

def next_block(the_file):
    """Return the next block of data from the trivia file."""
    category = next_line(the_file)

    question = next_line(the_file)

    answers = []
    for i in range(4):
        answers.append(next_line(the_file))

    correct = next_line(the_file)
    if correct:
        correct = correct[0]

    explanation = next_line(the_file)

    points = next_line(the_file)

    return category, question, answers, correct, explanation, points

def welcome(title):
    """Welcome the player and get his/her name."""
    print("		Welcome to Trivia Challenge!
")
    print("		", title, "
")

def main():
    trivia_file = open_file("trivia_points.txt", "r")
    title = next_line(trivia_file)
    welcome(title)
    score = 0

    # get first block
    category, question, answers, correct, explanation, points = next_block(trivia_file)
    while category:
        # ask a question
        print(category)
        print(question)
        for i in range(4):
            print("	", i + 1, "-", answers[i])

        # get answer
        answer = input("What's your answer?: ")

        # check answer
        if answer == correct:
            print("
Right!", end=" ")
            total = sum(points + points)
            score = total
        else:
            print("
Wrong.", end=" ")
        print(explanation)
        print("Score:", score, "

")

        points = int(points)

        # get next block
        category, question, answers, correct, explanation, points =       next_block(trivia_file)

    trivia_file.close()

    print("That was the last question!")
    print("You're final score is", score)

main()  
input("

Press the enter key to exit.")
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You're getting the points out as a string, and need to convert them to an int before doing any computation with it, ideally as soon as possible:

points = int(next_line(the_file))

in next_block should do the trick. Also, you are not adding the points to the score, you're replacing it.

total = sum(points + points)
score = total

should be

score += points

to add 'points' to 'score'.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...