Philly500
Philly500

Reputation: 41

Writing from python to text file

I'm trying to get the following to write the name and score to a text file. The name will write fine, but the score won't. Is there anything I have missed?

class Question:
     def __init__(self, prompt, answer):
          self.prompt = prompt
          self.answer = answer
print("General Quiz\n")
name = input("What's your name?")
print("Good Luck " + name + "!")
question_prompts = [
     "What is the capital of France?\n(a) Paris\n(b)London\n",
     "What color are bananas?\n(a) Red/Green\n(b)Yellow\n",
]

questions = [
     Question(question_prompts[0], "a"),
     Question(question_prompts[1], "b"),
]

def run_quiz(questions):
     score = 0
     for question in questions:
          answer = input(question.prompt)
          if answer == question.answer:
               score += 1
     print(name, "you got", score, "out of", len(questions))

run_quiz(questions)

file2write=open("testfile.txt",'a')
file2write.write(name + score)
file2write.close()

Upvotes: 0

Views: 175

Answers (6)

adist98
adist98

Reputation: 49

Making a variable global enables it's use in any function (speaking generally). The above statement can solve a lot of your problems while dealing with functions.

Upvotes: 0

user3480223
user3480223

Reputation: 54

 class Question:
      def __init__(self, prompt, answer):
           self.prompt = prompt
           self.answer = answer
 print("General Quiz\n")
 name = input("What's your name?")
 print("Good Luck " + name + "!")
 question_prompts = [
      "What is the capital of France?\n(a) Paris\n(b)London\n",
      "What color are bananas?\n(a) Red/Green\n(b)Yellow\n",
 ]

 questions = [
      Question(question_prompts[0], "a"),
      Question(question_prompts[1], "b"),
 ]

 def run_quiz(questions):
      score = 0
      for question in questions:
           answer = input(question.prompt)
           if answer == question.answer:
                score += 1
      print(name, "you got", score, "out of", len(questions))
      return score 
 score = run_quiz(questions)

 file2write=open("testfile.txt",'a')
 file2write.write("\n{} {}".format(name, score))

 file2write.close()

Upvotes: 2

reeshav
reeshav

Reputation: 1

Score is to be declared outside run_quiz function which will make it global to use. Alternatively just return the value of Score ,the 2nd is better as having global variable dangling around is not advisable.

Upvotes: 0

Nidhin Sajeev
Nidhin Sajeev

Reputation: 562

You need to return score from function run_quiz like this

def run_quiz(questions):
 score = 0
 for question in questions:
      answer = input(question.prompt)
      if answer == question.answer:
           score += 1
 print(name, "you got", score, "out of", len(questions))
 return score

score = run_quiz(questions)

also make score str while writing file2write.write(name + str(score))

Upvotes: 0

Max Carroll
Max Carroll

Reputation: 4839

There are a few mistakes in here,

You must declare score outside the scope of the function, and use the global keyword inside the function to let the function know that you want to use the global version of the variable.

Also you need to cast score to a string

score = 0

class Question:
     def __init__(self, prompt, answer):
          self.prompt = prompt
          self.answer = answer
print("General Quiz\n")
name = input("What's your name?")
print("Good Luck " + name + "!")
question_prompts = [
     "What is the capital of France?\n(a) Paris\n(b)London\n",
     "What color are bananas?\n(a) Red/Green\n(b)Yellow\n",
]

questions = [
     Question(question_prompts[0], "a"),
     Question(question_prompts[1], "b"),
]

def run_quiz(questions):
     global score
     for question in questions:
          answer = input(question.prompt)
          if answer == question.answer:
               score += 1
     print(name, "you got", str(score), "out of", len(questions))

run_quiz(questions)



file2write=open("testfile.txt",'a')
file2write.write(name + str(score))
file2write.close()

Upvotes: 1

Richard Green
Richard Green

Reputation: 2062

score is only scoped within the run_quiz function - you need to make it a global for it to be available in the penultimate line

Upvotes: 2

Related Questions