首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用%run file.py保存答案

使用%run file.py保存答案
EN

Stack Overflow用户
提问于 2020-09-04 03:25:22
回答 1查看 20关注 0票数 0

我目前正在循环中运行一组文件,并检查它们的答案。目前,我希望存储使用%run file_name.py运行文件时初始化的get变量

我在下面附上了两个例子,一个是文件本身,它有3个答案。第二个是我用来比较它们的代码。

代码语言:javascript
复制
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)
代码语言:javascript
复制
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等。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-09-04 03:44:42

如果你的文件只给出答案作为输出,你可以使用

代码语言:javascript
复制
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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63730398

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档