Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 需要打开/创建文本文件的帮助吗_Python - Fatal编程技术网

Python 需要打开/创建文本文件的帮助吗

Python 需要打开/创建文本文件的帮助吗,python,Python,我需要关于打开文本文件的帮助。由于某种原因,它似乎不能正常工作,我想知道是否有人能指出任何错误并告诉我如何解决它们。谢谢 import time import operator import random username=input("What is your name?") usersClass = input("Which class are you in, 1, 2 or 3?") print ("Welcome {} to the Math quiz, hope you have

我需要关于打开文本文件的帮助。由于某种原因,它似乎不能正常工作,我想知道是否有人能指出任何错误并告诉我如何解决它们。谢谢

import time
import operator
import random

username=input("What is your name?")

usersClass = input("Which class are you in, 1, 2 or 3?")
print ("Welcome {} to the Math quiz, hope you have fun lets begin".format(username))

start = time.time()

def askquestion():
  score = 0
  opslist = {operator.add: "+", operator.sub: "-", operator.mul: "x"}
  num1,num2 = random.randint(1,10), random.randint(1,10)    
  ops = random.choice(list(opslist.keys()))                       
  ActualAnswer = (ops(num1,num2))                               
  score = 0
  print(num1,opslist[ops],num2)            
  userAns = (int(input("Answer the above")))
  if userAns == ActualAnswer:   
    print("Awesome, that's correct")
    return 1
  else:
    print("Incorrect sorry")
    score = score - 0
  return 0

totalScore = 0
for i in range (10):
  totalScore += askquestion()  

print ("Well done, you have completed the quiz")

print("your final score was " + str(totalScore))
if totalScore > 9:
    print("Great work! You got top marks!")
elif totalScore > 7:
   print("Good, try harder next time and you might get full marks!")
elif totalScore > 5:
    print("You did alright")
elif totalScore > 4:
    print("Are you not motivated.. c'mon")
else:
    print("Hmmm..dissapointing, however know you know what you need to work on")

end = time.time()
etime = end - start
timeTaken = round(etime)

if usersClass == 1:
    with open("class1.txt","a+") as f:
        f.write("{}:Scored {} in {} seconds".format(username,totalScore,timeTaken))

elif usersClass == 2:
    with open("class2.txt","a+") as f:
        f.write("{}:Scored {} in {} seconds".format(username,totalScore,timeTaken))

elif usersClass == 3:
    with open("class3.txt","a+") as f:
        f.write("{}:Scored {} in {} seconds".format(username,totalScore,timeTaken))
所以看起来有几件事你可能需要改变。您可能应该使用原始输入来捕获用户的输入

usersClass的if语句可能不会触发,因为它们查找的是int而不是输入中传递的数字字符串

您可以将这些输入转换为int:usersClass=int(usersClass),也可以在if语句中的int周围添加引号:if usersClass==“1”:

此外,第一个if语句中似乎存在无意的缩进


编辑:刚刚意识到您可能正在使用Python3.x,无需将输入更改为原始输入,因为它在Python3中不存在……我的错误。

您必须确保
用户类的值不小于1或大于3。您从来没有这样做过,这就是为什么如果您输入的数字不符合这些条件,文件将无法打开或创建


我不明白
askquestion
最后一行上方的
score=score-0
的意思。这是什么意思?如果您不想更改变量的值,请不要这样做。

“工作不正常”没有帮助。发生了什么,出现了什么错误?代码的最后一部分从if usersclass==1:etc开始;我需要它来打开一个文件,显示用户的分数和所花的时间。关于代码的开头,我想知道我是否输入了任何错误,因为在python上,最后一节似乎没有显示/打开文件