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


i'm getting error when reading file:

line 70 in main: score += points typeerror: unsupported operand type(s) +=: 'int' , 'str' 

i'm taking integer in file , adding variable score. reading file done in next_line function called in next_block function.

i have tried converting both score , points integer doesn't seem work.

here's program code:

# trivia challenge # trivia game reads plain text file  import sys  def open_file(file_name, mode):     """open file."""     try:         the_file = open(file_name, mode)     except ioerror e:         print("unable open file", file_name, "ending program.\n",e)         input("\n\npress enter key exit.")         sys.exit()     else:         return the_file  def next_line(the_file):     """return next line trivia file, formatted."""     line = the_file.readline()     line = line.replace("/", "\n")     return line  def next_block(the_file):     """return next block of data trivia file."""     category = next_line(the_file)      question = next_line(the_file)      answers = []     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 player , his/her name."""     print("\t\twelcome trivia challenge!\n")     print("\t\t", title, "\n")  def main():     trivia_file = open_file("trivia.txt", "r")     title = next_line(trivia_file)     welcome(title)     score = 0      # first block     category, question, answers, correct, explanation, points = next_block(trivia_file)     while category:         # ask question         print(category)         print(question)         in range(4):             print("\t", + 1, "-", answers[i])          # answer         answer = input("what's answer?: ")          # check answer         if answer == correct:             print("\nright!", end= " ")             score += points         else:             print("\nwrong.", end= " ")         print(explanation)         print("score:", score, "\n\n")          # next block         category, question, answers, correct, explanation, points = next_block(trivia_file)      trivia_file.close()      print("that last question!")     print("your final score is", score)  main() input("\n\npress enter key exit.") 

points string, because read file:

points = next_line(the_file) 

but score integer:

score = 0 

you can't add string integer. if value read file represents integer number, need convert first, using int():

score += int(points) 

Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -