-3

I am trying to print a variable of a score and name to a .txt file using python.

import random
import csv
import operator
import datetime


now = datetime.datetime.now() ## gets the exact time of when the user begins the test.

def main():
    global myRecord
    myRecord = []
    name = getNames()
    myRecord.append(name)
    record = quiz()


def getNames(): ## this function grabs first and lastname of the user
    firstName = input ("Please enter your first name") ## asks for users name
    surName = input("Please enter your surname") ## asks for users suername
    space = " "
    fullName =  firstName + space +surName ## puts data of name together to  make full name
    print("Hello")
    print (fullName)
    myRecord.append(fullName)
    return fullName ## this is a variable returned to main

def quiz():
    print('Welcome. This is a 10 question math quiz\n')
    score = 0 ## sets score to 0.
    for i in range(10): ## repeats question 10 times
        correct = askQuestion()## if the statement above if correct the program asks a question.
        if correct:
            score += 1## adds one to the score
            print('Correct!\n')## prints correct if the user gets a question correct.
        else:
            print('Incorrect!\n') ## prints incorrect if the user gets a question wrong.
            return 'Your score was {}/10'.format(score)

def randomCalc():
    ops = {'+':operator.add, ## selects one of the three operators
           '-':operator.sub, ## selects one of the three operators
           '*':operator.mul,} ## selects one of the three operators
    num1 = random.randint(0,12)    ## samples a number between 0 and 12
    num2 = random.randint(1,10)   ## zero are not used to stop diving by zero
    op = random.choice(list(ops.keys()))
    answer = ops.get(op)(num1,num2)
    print('What is {} {} {}?\n'.format(num1, op, num2))  ## puts together the num1, the operator and num2 to form question
    return answer

def askQuestion():
    answer = randomCalc()
    guess = float(input())
    return guess == answer

def myfileWrite (myrecord):
    with open('Namescore.txt', 'w') as score:
        score.write(fullName + '\n')




main()

here is the full code it should ask the users name, print 10 maths questions and then save the time name and score to a txt file if you can help please do many thanks

Alexander Vogt
  • 17,879
  • 13
  • 52
  • 68
Rasnaamt
  • 21
  • 1
  • 1
  • 4
  • your last edit removed all code from the question. You should not do this as now it is impossible to understand what your question was about in the first instance – Erik Mar 28 '15 at 13:11

2 Answers2

2

Your indentation is incorrect and you never actually call the function:

with open('Namescore.txt', 'w') as score:
    score.write(fullName + '\n')
Malik Brahimi
  • 16,341
  • 7
  • 39
  • 70
0

the code you wrote would IIRC re-make the file each time you ran the code. I believe this is the correct way to do it:

with open("Namescore.txt", "a") as file:
    file.write(name, score, "\n")  # I don't know what your vars are called

This will append to the file rather than rewrite :)

If you want to do it your way, the correct way would be:

def writeScore(name, score):
    file = open("Namescore.txt", "a")
    file.write(name, score, "\n")
    file.close()

writeScore("Example Examlpus", 201)
Fredrik
  • 971
  • 8
  • 23