将变量附加到文本文件无法在Python中运行

E

我试图让我的程序在完成某些操作后将两个变量输出到文本文件,但我所得到的只是文件夹,其中没有文本文件。这是有问题的代码:

我进行了编辑,以包括整个程序。

import random #needed for the random question creation
import os #needed for when the scores will be appended to a text file
#import csv #work in progress
answer = 0 #just to ensure a strage error isn't created by the input validation (since converting to a global variable isn't really needed)
global questionnumber #sets the question number to be a global variable so that 
questionnumber = 0
global score
score = 0
global name
name = "a"
def check_input(user_input): #used to verify that input by user is a valid number
    try: #the condition that does the checking
        answer = int (user_input)
    except ValueError: #what to do if the number isn't actually a number
        return fail() #calls the failure message
    return answer #sends the answer back to the rest of the program
def fail(): #the failure message procedure defined
   print("That isn't a whole number! Try again with a different question") #tells the user what's going on
   global questionnumber #calls the global variable for this procedure
   questionnumber = questionnumber - 1 #allows the user, who made a mistake, a second chance to get it right
def questions(): #the question procedure defined
    global name
    name=input("Enter your name: ") #gets the user's name for logging and also outputting messages to them
    print("Hello there",name,"! Please answer 10 random maths questions for this test!") #outputs welcome message to the user
    ClassOfStudent=input("Which class are you in?") #gets the user's class for logging
    finish = False
    while finish == False: #only occurs if "finish" isn't set to true so that the questions asked don't exceed ten
        global questionnumber #calls the global variable
        global score
        choice = random.choice("+-x") #uses the random function to choose the operator
        if questionnumber < 10 | questionnumber >= 0: #validation to ensure the question number is within ten
            number1 = random.randrange(1,12) #uses random numbers from the random function, above 1 and below 12
            number2 = random.randrange(1,12) #same as the abovem for the second number
            print((number1),(choice),(number2)) #outputs the sum for the student to answer
            answer=check_input((input("What is the answer?"))) #asks for the student's answer
            questionnumber = questionnumber + 1 #adds one to the numebvr of questions asked

            if choice==("+"): #if the ramdomly generated operator was plus
                correctanswer = number1+number2 #operator is used with the numbers to work out the right answer
                if answer==correctanswer: #checks the studen't answer is right
                    print("That's the correct answer") #if it is, it tells the student
                    score = score + 1 #adds one to the score that the user has
                else:
                    print("Wrong answer, the answer was",correctanswer,"!") #if the answer is wrong, it tells the student and doesn't add one to the score

            if choice==("x"): #essentially the same as the addition, but with a multiplicatin operator instead
                correctanswer = number1*number2
                if answer==correctanswer:
                    print("That's the correct answer")
                    score = score + 1
                else:
                    print("Wrong answer, the answer was",correctanswer,"!")

            elif choice==("-"): #essentially the same as the addition, but with a subtraction operator instead
                correctanswer = number1-number2

                if answer==correctanswer:
                    print("That's the correct answer")
                    score = score + 1
                else:
                    print("Wrong answer, the answer was",correctanswer,"!")
        else: #if the number of questions asked is ten, it goes on to end the program
            finish = True
    else:
            print("Good job",name,"! You have finished the quiz") #outputs a message to the user to tell them that they've finished the quiz
            print("You scored " + str(score) + "/10 questions.") #ouputs the user's score to let them know how well they've done
            #all below here is a work in progress
            #Create a new directory for the scores to go in, if it is not there already.
            if os.path.exists("Scores") == False:
              os.mkdir("Scores")
            os.chdir("Scores")
            if ClassOfStudent==1: #write scores to class 1 text
                file_var = open("Class 1.txt",'w+')
                file_var.write("name, score")
                file_var.close()
            if ClassOfStudent==2: #write score to class 2 text
                file_var = open("Class 2.txt",'w+')
                file_var.write(name, score)
                file_var.close()
            if ClassOfStudent==3: #write score to class 3 text
                file_var = open("Class 3.txt",'w+')
                file_var.write(name, score)
                file_var.close()
questions()

邓肯

使用print(repr(ClassOfStudent))显示的值和类型:

>>> ClassOfStudent = input("Which class are you in?")
Which class are you in?2
>>> print(repr(ClassOfStudent))
'2'

input()在Python 3中使用会以字符串形式读取值。您需要将其转换为int或对字符串进行比较。

ClassOfStudent = int(input("Which class are you in?")

应该解决您的问题。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

将“引用变量调用”附加到文本文件

来自分类Dev

将文本附加到我的Google驱动器上的文本文件(Python)

来自分类Dev

使用PowerShell将文本附加到文本文件中的某些值

来自分类Dev

将标题附加到文本文件

来自分类Dev

将数据从List <T>附加到文本文件

来自分类Dev

Python-将文本文件中的变量求和

来自分类Dev

将文本文件的内容附加到C ++中的另一个文件

来自分类Dev

首次写入时覆盖文本文件,然后附加到文本文件-Python

来自分类Dev

首次写入时覆盖文本文件,然后附加到文本文件-Python

来自分类Dev

python覆盖并附加到文本文件

来自分类Dev

将新行附加到文本文件,然后在 Python 3 之后检索它们

来自分类Dev

将列表<T>中的数据附加到文本文件

来自分类Dev

将字符串连接/附加到文本文件中的数字列表

来自分类Dev

我如何将文本文件附加到我的contenteditable div中?

来自分类Dev

将文本文件中的报价附加到每一行

来自分类Dev

如何将值附加到empy文本文件中

来自分类Dev

如何将 numpy x 和 y 值附加到文本文件中

来自分类Dev

附加到文本文件VBA

来自分类Dev

附加到文本文件

来自分类Dev

附加到文本文件的回声

来自分类常见问题

在小括号{}中附加到Python文本文件中的字典

来自分类Dev

在小括号{}中附加到Python文本文件中的字典

来自分类Dev

通过将多个文本文件附加到summary_ [date] .log,在字符串和副本行中搜索多个文本文件

来自分类Dev

mac:在许多目录中串联文本文件的内容;将目录名称附加到相应的文本文件内容

来自分类Dev

Python:将数据存储在文本文件中并附加特定/单独的行

来自分类Dev

python在文本文件中搜索字符串并将值添加到变量

来自分类Dev

是否真的可以通过使用CFile和CStdio类将数据附加到MFC中的文本文件中?

来自分类Dev

是否真的可以通过使用CFile和CStdio类将数据附加到MFC中的文本文件中?

来自分类Dev

将文本文件中的 python 代码运行到你的 python 程序中

Related 相关文章

  1. 1

    将“引用变量调用”附加到文本文件

  2. 2

    将文本附加到我的Google驱动器上的文本文件(Python)

  3. 3

    使用PowerShell将文本附加到文本文件中的某些值

  4. 4

    将标题附加到文本文件

  5. 5

    将数据从List <T>附加到文本文件

  6. 6

    Python-将文本文件中的变量求和

  7. 7

    将文本文件的内容附加到C ++中的另一个文件

  8. 8

    首次写入时覆盖文本文件,然后附加到文本文件-Python

  9. 9

    首次写入时覆盖文本文件,然后附加到文本文件-Python

  10. 10

    python覆盖并附加到文本文件

  11. 11

    将新行附加到文本文件,然后在 Python 3 之后检索它们

  12. 12

    将列表<T>中的数据附加到文本文件

  13. 13

    将字符串连接/附加到文本文件中的数字列表

  14. 14

    我如何将文本文件附加到我的contenteditable div中?

  15. 15

    将文本文件中的报价附加到每一行

  16. 16

    如何将值附加到empy文本文件中

  17. 17

    如何将 numpy x 和 y 值附加到文本文件中

  18. 18

    附加到文本文件VBA

  19. 19

    附加到文本文件

  20. 20

    附加到文本文件的回声

  21. 21

    在小括号{}中附加到Python文本文件中的字典

  22. 22

    在小括号{}中附加到Python文本文件中的字典

  23. 23

    通过将多个文本文件附加到summary_ [date] .log,在字符串和副本行中搜索多个文本文件

  24. 24

    mac:在许多目录中串联文本文件的内容;将目录名称附加到相应的文本文件内容

  25. 25

    Python:将数据存储在文本文件中并附加特定/单独的行

  26. 26

    python在文本文件中搜索字符串并将值添加到变量

  27. 27

    是否真的可以通过使用CFile和CStdio类将数据附加到MFC中的文本文件中?

  28. 28

    是否真的可以通过使用CFile和CStdio类将数据附加到MFC中的文本文件中?

  29. 29

    将文本文件中的 python 代码运行到你的 python 程序中

热门标签

归档