我的代码不会将输入保存到文件中

时间:2015-04-15 12:32:10

标签: python saving-data

我的代码不会保存文件

#ask for name
name=input("what is your name?")
print("start your quiz " + name + "!")
user=input
class123=('1','2','3')
class123=input("what class are you in 1,2 or 3") 
#get random number
import random
#ask number of questions you want
score=0
count=10
#start while loop
while count != 0:
   num1=random.randint(1,8)
   num2=random.randint(1,8)
   symbol=random.choice(['*','+','-'])
   if symbol=="*":
      user=int(input(str(num1) + "*" + str(num2)))
      count=count-1
      answer=num1 * num2
      if user==answer:
         print("well done your score goes up by 1")
         score=score+1
      else:
         print("this is wrong next question")
   elif symbol=="+":
      user=int(input(str(num1) + "+" + str(num2)))`enter code here`
      count=count-1
      answer=num1 + num2
      if user==answer:
         print("well done your score goes up by 1")
         score=score+1
      else:
         print("this is wrong next question")
   elif symbol=="-":
      user=int(input(str(num1) + "-" + str(num2)))
      count=count-1
      answer=num1 - num2
      if user==answer:
         print("well done your score goes up by 1")
         score=score+1
      else:
         print("this is wrong next question")
#get final score
print(" your score was " + str(score) + " well done ")
#save data into 3 classes

while score == 10 and score >= 0:
    if class123 == '1':
      inFile = open("Class1.txt", 'a')
      inFile.write("\n" + name + ":" + str(score))
      inFile.close()
      score = -1
    elif class123 == '2':
      inFile = open("Class2.txt", 'a')
      inFile.write("\n" + name + ":" + str(score))
      inFile.close()
      score = -1
    elif class123 == '3':
      inFile = open("Class3.txt", 'a')
      inFile.write("\n" + name + ":" + str(score))
      inFile.close()
      score = -1

1 个答案:

答案 0 :(得分:0)

如果您希望代码正常工作,请先删除或注释掉第26行的“输入代码”部分。可以使用“#”完成。原因是''是一个字符串,将被解释为一个不应该出现的字符串。

user=int(input(str(num1) + "+" + str(num2))) #enter code here

第二,它没有保存,因为虽然从未运行,因为虽然得分== 10不是真的,所以你写分数的循环永远不会运行。

您可以将其更改为:

while score <= 10 and score >= 0:

我测试了我做的2个调整,好吧,你的代码会运行并创建Class?.txt文件

然而正如其他人所说,这不是一个正确的结构。不太适合使用的东西。所以你可能想要改进你的代码,但在这里我只是给你一些修复如何让它运行,如果你不介意的话。