简体   繁体   English

python:数学测验-数据未存储

[英]python: Maths quiz- data not being stored

My task is to create a quiz for primary school children. 我的任务是为小学生创建测验。 The quiz bit works fine. 测验位工作正常。 But I must time how long the child takes and store their 'username' 'correctAnswers' and 'timeTaken' into a .txt file for the specific class the child is in. To do that I ask the child their class number and store their information into the file that was specifically made for that class. 但是我必须确定孩子需要花多长时间,并将他们的“用户名”,“ correctAnswers”和“ timeTaken”存储到该孩子所在的特定班级的.txt文件中。为此,我请孩子提供其班级编号并存储他们的信息到专门为该类制作的文件中。 The problems I in counter are: 我要解决的问题是:

The time isnt being rounded even though I have timeTaken = round(etime)in my code 即使我的代码中有timeTaken = round(etime),也不会四舍五入时间

raw_input not being defined (I have no idea how else to define it) raw_input未定义(我不知道该如何定义它)

The message "Sorry, we can not save your data as the class you entered is not valid." 消息“对不起,由于您输入的类别无效,我们无法保存您的数据。” comes up even when a valid class number has been entered. 即使输入了有效的班级号也会出现。

Ive searched everywhere but with no luck. 我已经到处搜寻,但是没有运气。 Any help at all would be greatly appreciated. 任何帮助将不胜感激。

import time
import random
import math

def test():
    num1=random.randint(1, 10)
    num2=random.randint(1, num1)

    ops = ['+','-','*']
    operation = random.choice(ops)

    num3=int(eval(str(num1) + operation + str(num2)))

    print ("What is {} {} {}?".format(num1, operation, num2))
    userAnswer= int(input("Your answer:"))
    if userAnswer != num3:
        print ("Incorrect. The right answer is {}".format(num3))
        return False
    else:
        print("correct")
        return True

username=input("What is your name?")
print ("Welcome {} to the Arithmetic quiz".format(username))
usersClass = input("Which class are you in? (1,2 or 3)")
raw_input("Press Enter to Start...")
start = time.time()
correctAnswers=0
for question_number in range(10):
    if test():
        correctAnswers +=1

print("{}: You got {} answers correct".format(username, correctAnswers))
end = time.time()
etime = end - start
timeTaken = round(etime)
print ("You completed the quiz in {} seconds".format(timeTaken))
if usersClass == 1:
    with open("class1.txt","a+") as f:
        f.write("{}:Scored {} in {} seconds".format(username,correctAnswers,timeTaken))

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

elif usersClass == 3:
    with open("class3.txt","a+") as f:
        f.write("{}:Scored {} in {} seconds".format(username,correctAnswers,timeTaken))
else:
    print("Sorry, we can not save your data as the class you entered is not valid.")

The return value of input is a str object: input的返回值是一个str对象:

>>> usersClass = input("Which class are you in? (1,2 or 3)")
Which class are you in? (1,2 or 3)3
>>> type(usersClass)
<class 'str'>

As a result, your subsequent checks against int objects will evaluate to False (ie, '3' != 3 ) resulting in what you are seeing. 结果,您随后对int对象的检查将得出False (即'3' != 3 ),结果是您看到的内容。

The conditions of comparing which usersClass the user has selected would need to compare the same type to ensure equality. 比较用户选择哪个usersClass的条件将需要比较相同类型以确保相等。 This means you could convert your return value of input to an int and continue to compare usersClass to an int which would satisfy your comparison as your code is written now, 这意味着您可以将input的返回值转换为int并继续将usersClassint进行比较,这将满足您现在编写代码时的比较要求,

usersClass = int(input("Which class are you in? (1,2 or 3)"))

or change the conditionals to compare usersClass to the str representation of 1 , 2 and 3 . 或改变条件句比较usersClassstr的表示123

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

As to the problem you are experiencing with raw_input using Python 3, it has been renamed to input : (taken from What's New in Python 3.0 ) 关于您使用Python 3使用raw_input遇到的问题,它已重命名为input :(摘自Python 3.0的新增功能

PEP 3111: raw_input() was renamed to input(). PEP 3111:raw_input()重命名为input()。 That is, the new input() function reads a line from sys.stdin and returns it with the trailing newline stripped. 也就是说,新的input()函数从sys.stdin中读取一行,并以结尾的换行符删除后返回它。 It raises EOFError if the input is terminated prematurely. 如果输入过早终止,则会引发EOFError。 To get the old behavior of input(), use eval(input()). 要获取input()的旧行为,请使用eval(input())。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM