2016-04-13 7 views
0

私のプログラミングクラスでは、各質問ごとに独立したポイント値を持つトリビアプログラムを作成する必要があります。これを行うには、私は、テキストファイルに行としてポイントを追加し、コードに次の行を持っている:ValueError:基数10のint()に無効なリテラルを続ける: ''エラー、プログラム全体で動作していますが

points = int(next_line(the_file)) 

私も持っている:

if answer == correct: 
    print("\nRight!") 
    score += points 

合計にポイントを追加するために、スコア。 これはプログラム中のすべてで機能し、プレイ後にポイントを追加しますそれぞれの質問を正しいものにします。しかし、最後のスコアを表示した後、プログラムの最後に値のエラーがあると表示されます。 ValueError:基数10のint()のリテラルが無効です エラーが原因ですそれがプログラム全体を通して、最後まで終わりまで働いているという事実に繋がります。

ご協力いただきますようお願い申し上げます。ありがとうございます。ここで

は、全体のコードです:

import sys 
choice = None 
while choice != "0": 
    choice = input(""" 
WELCOME TO THE TRIVIA CHALLENGE. PLEASE SELECT AN EPISODE 
    0: Exit Game 
    1: Trivia 1 
    2: Trivia 2 

    Choice: """) 
    if choice == "0": 
     print("You have chose to exit the program. Your loss.") 
     sys.exit() 
    elif choice == "1": 
     fileChoice = "trivia1.txt" 
    elif choice == "2": 
     fileChoice = "trivia2.txt" 
    else: 
     print("You have entered an invalid option. You are being kicked off.") 
     sys.exit() 

    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.\n", e) 
      input("\n\nPress 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("/", "\n") 
     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 = int(next_line(the_file)) 

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

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

    def main(): 
     trivia_file = open_file(fileChoice, "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("\t", i + 1, "-", answers[i]) 

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

      # check answer 
      if answer == correct: 
       print("\nRight!") 
       score += points 
      else: 
       print("\nWrong.") 
      print(explanation) 
      print("Score:", score, "\n\n") 

      # get next block 
      category, question, answers, correct, explanation, points = next_block(trivia_file) 
     trivia_file.close() 
     print("That was the last question!") 
     print("Your final score is", score) 

    main() 
input("\n\nPress the enter key to exit.") 

私が持っているもう一つの問題は、それはまた、動作しませんので、私は、)(sys.exitを行った方法です。

+0

あなたはより多くのコードを表示することができます:

ので、ちょうど空白行があるかどうかを確認? –

+0

退室条件とは何ですか?それは最終的に起こるので、おそらくあなたがプログラムを終了する方法に関連していますが、それ以上のコードはなく、それは単なる推測です。 –

+0

私はコード全体を追加しました。うまくいけば助けてください。 – Michael

答えて

0

空白行がある場合、このエラーが発生します。あなたは、エラーメッセージを見れば(それは明らかではないかもしれませんが)、その問題が何であるかを伝える:10:

ValueError: invalid literal for int() with base 10: 

ブランクが手掛かり、ファイル内の実際の空白行です。

points_line = next_line(the_file).strip() 
if points_line: 
    points = int(points_line) 
else: 
    print('Got a blank line when expecting points!') 
+0

もしあなたがテキストファイルで話しているのであれば、私はすでに何度もそれを見てきましたが、そこには空白がありません。すべてが正しくフォーマットされています。 – Michael

+0

はい、 'next_line'メソッドは空行を返しています。ファイル内の実際の行に '/'のみが含まれている場合は、新しい行の文字であるメソッドから '\ n'を返します。実際には空白行になります。 –

+0

私はこれを実際によく理解していないので、説明できますか?私はそれを見ているので、next_lineメソッドは、テキストファイルの次の行を読み取ります。空白行がない場合、空白行を返す方法はありますか? – Michael

関連する問題