簡體   English   中英

使用 %run file.py 保存答案

[英]Save answers using %run file.py

我目前正在循環中運行一組文件並檢查它們的答案。 目前,當我使用%run file_name.py運行文件時,我想存儲初始化的變量

我在下面附上了兩個例子,一個是文件本身,它是 3 個答案。 第二個是我用來比較它們的代碼。

s = 'Amsterdam'
# Print out 'e' using indexing
answer1 = s[2]
print(answer1)

s ='Amsterdam'
# Reverse the string using slicing
answer2 = s[1]
print(answer2)

answer3 = s[::-1]
print(answer3)

for i in range(1,num_questions+1):
    %run {i}.py
    answers = # HOW DO I MAKE LISTS BASED ON THE NUMBER OF VALUES RETURNED
    if manual == 'N':
        correct_answer_key,question = BitGrading.mannual_answer_key(1)
        print(f'\n The correct answer key for {question} is {correct_answer_key}')
        Grading.grade_question('Q1',student_ids,grade_sheet,answers,correct_answer_key[0])
        print(f'\n{grade_sheet.loc[grade_sheet.ID == student_ids[0]]}')
    else:   
        Grading.grade_question('Q1',student_ids,grade_sheet,answers,correct_answer[0])
        grade_sheet.loc[grade_sheet.ID == student_ids[0]]

我的主要重點是根據返回的變量數量自動將答案存儲在列表中。 答案的格式將始終相同,即 answer1、answer2、answer3 等。

如果您的文件只給出答案作為輸出,您可以使用

import subprocess

output = subprocess.check_output(['python', '%pathToPYFile%']) # executes the file and saves the output
answers = output.decode('utf-8').split('\r\n')[:-1] # deletes an empty element at the end
print(answers)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM