从python写入文本文件

时间:2018-06-25 08:37:38

标签: python python-3.x

我正在尝试获取以下内容,以将名称和分数记入文本文件。名字会写得很好,但分数不会。有什么我想念的吗?

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()

6 个答案:

答案 0 :(得分:1)

score仅限于run_quiz函数中-您需要使其成为全局变量,以便倒数第二行可用

答案 1 :(得分:1)

 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()

答案 2 :(得分:0)

您需要从函数score返回run_quiz 像这样

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)

在撰写str时也得分file2write.write(name + str(score))

答案 3 :(得分:0)

这里有一些错误,

您必须在函数范围之外声明score,并在函数内部使用global关键字,以使函数知道您要使用变量的全局版本。

还需要将乐谱转换为字符串

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()

答案 4 :(得分:0)

分数将在run_quiz函数外部声明,这将使其成为全局使用。 或者,仅返回Score的值,第二个比较好,因为不建议使用全局变量。

答案 5 :(得分:0)

全局设置变量使它可以在任何函数中使用(一般而言)。 上面的语句可以解决函数处理中的许多问题。