2015-01-14 4 views
-2

Мне нужно добавить имя и оценку к определенной папке и файлу в конце программы, print («Вы забили» + str (правильные запросы) + "/ 10 вопросов. ») Студент выбирает, в каком классе они находятся, после окончания вопросов, которые я хочу, чтобы он писал в свою папку классов и текстовый файл, они назвали и оценили.добавить значения переменных в определенную папку/файл

import random 
import time 
import sys 

def questions(): 
    name=input("What is your name: ") 
    print("Hello there",name,"!") 
    time.sleep(0.5) 
    print("What class are you in?") 
    time.sleep(0.5) 
    print("")#space 
    whatClass=input("Enter 1, 2 or 3: ") 
    print("")#space 
    if whatClass == "1": 
     print("You are in class 1!") 
    elif whatClass =="2": 
     print("You are in class 2!") 
    elif whatClass =="3": 
     print("you are in class 3!") 
    else: 
     print ("Please try again") 
     quit() 

    finish = False 
    questionnumber = 0 
    correctquestions = 0 

    while not finish: 
     choice = random.choice("+-x") 
     if questionnumber < 10 | questionnumber >= 0: 
      number1 = random.randrange(1,10) 
      number2 = random.randrange(1,10) 
      print((number1),(choice),(number2)) 
      answer=int(input("What is the answer?: ")) 
      questionnumber = questionnumber + 1 

      if choice==("+"): 
       realanswer = number1+number2 
       if answer==realanswer: 
        print("That's the correct answer") 
        correctquestions = correctquestions + 1 
       else: 
        print("Wrong answer, the answer was",realanswer,"!") 

      if choice==("x"): 
       realanswer = number1*number2 
       if answer==realanswer: 
        print("That's the correct answer") 
        correctquestions = correctquestions + 1 
       else: 
        print("Wrong answer, the answer was",realanswer,"!") 

      elif choice==("-"): 
       realanswer = number1-number2 

       if answer==realanswer: 
        print("That's the correct answer") 
        correctquestions = correctquestions + 1 
       else: 
        print("Wrong answer, the answer was",realanswer,"!") 
     else: 
      finish = True 
    else: 
      print("Good job",name,"! You have finished the quiz") 
      print("You scored " + str(correctquestions) + "/10 questions.") 
      #if the class is 1 append the name and score to folder class1 
      #if the class is 2 append the name and score to folder class2 
      #if the class is 3 append the name and score to folder class3 


questions() 
+0

Не могли бы вы уточнить, каков ваш вопрос, пожалуйста? – jonrsharpe

+0

Я хочу, чтобы имя и правильное количество запросов сохранялось в папке/файле. – ruzter

+0

Это не совсем разъясняет, что вы просите. См. Http://stackoverflow.com/help/how-to-ask – jonrsharpe

ответ

0

Я думаю, что вы хотите что-то вроде этого:

import errno 

folder= 'class%s'%whatClass 
# create the folder if it does not exist 
try: 
    os.makedirs(folder) 
except OSError as exception: 
    if exception.errno != errno.EEXIST: 
     raise 

# create a file with the student's name in the folder, and write the score 
with open(os.path.join(folder, name+'.txt'), 'w') as f: 
    f.write(str(score)) 

Кроме того, пожалуйста, прочитайте the python docs on file IO.

Смежные вопросы