0

I'm trying to automate some of the grading process for a beginners Python class. The students provide a Python program that prompts the user for 5 items and the monthly amount for those 5 items. The students then display a budget displaying the items, the monthly amount, and the yearly amount. I would like to write a python script that will execute all 65 students programs and have the program provide the input for those 5 items and the monthly amount. I would like the input variables to be random from a list and random float amount. Does this seem feasible to do from the program or do the variables and monthly amounts need to be provided from the command line? The overall goal is to provide automatic input values for the 5 items and the amount and then the student's print statements will be printed to the console, making it quicker to provide a grade based on the student's output.

Right now, my code prompts the teacher for the path to the directory where the student's assignments are held and stores each file path in a list. Now, I'm trying to figure out how to execute each Python file from the list and when each student's program prompts for the user input, my script will provide the input automatically. The prompt strings are specific which allows me to specify an item vs an amount. EG. "Please enter item 1:" (input item name), "Please enter item amount: " (input float amount)

This is my Pseudo Code:

import os

list_files = ["student1.py", "student2.py" ,....]

list_items = ["mortgage", "groceries", "insurance"]

list_amounts = [123.34, 1024.11, 32.3]

Now I need to figure out how to execute each file and have the program provide the input from list_items and list_amounts. I'm looking for advice on the best way to approach this. I would prefer to have the program provide the input from these lists and not have the teacher type in the values as you would through the arguments.

For example, python student1.py mortgage 123.34 groceries 32.3 insurance 1024.11.

How can I do this? Preferably not through the argv [ ]

Brad Rydalch
  • 61
  • 1
  • 1
  • 6
  • I'm not a pro at python and also still learning but maybe you could try to code an asynchronous function which runs whenever your code is executed and the function is responsible for putting random numbers into the input prompts – newbiedude Feb 11 '19 at 22:49
  • I would say my concrete question is "how can I provide input from a list in my python program into another python program?" I hope that helps. – Brad Rydalch Feb 11 '19 at 23:03
  • 1
    it's relatively easy to list all projects in a folder and execute them using `subprocess`, the only tricky part is going to be submitting the variables: if each program expects a different input type you're not going to be able to automate this – Nathan Feb 11 '19 at 23:10
  • This question may be of use to you https://stackoverflow.com/questions/10673185/python-execute-command-line-sending-input-and-reading-output – Nathan Feb 11 '19 at 23:11
  • Each program has the same input questions. "enter item 1: " , "enter item amount: ", "enter item 2: ", "enter item amount: "These 2 questions are asked in every file in the same order up to 5 items. The same 5 input strings and float values can be the same for all the submitted files. – Brad Rydalch Feb 11 '19 at 23:15
  • Thanks Nathan, I'll give that a look. – Brad Rydalch Feb 11 '19 at 23:16
  • Thank you everyone for your help and suggestions. I was able to execute the student's program from my grader program. Nowe i"m just trying to get my program to provide the input automatically. – Brad Rydalch Feb 15 '19 at 18:13

2 Answers2

2

I assume the problem is the call of the python program from python, not making the mortgage 123.34 groceries 32.3 insurance 1024.11 part.

Here is one option:

import subprocess
import shlex

for script in ["student1.py", "student2.py"]:
    command = 'python {} mortgage 123.34 groceries 32.3 insurance 1024.11'.format(script)
    proc = subprocess.Popen(shlex.split(command), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    stdout, stderr = proc.communicate()
    print stdout
    print stderr

See many more options in https://stackoverflow.com/a/92395/6352677.

Keldorn
  • 1,980
  • 15
  • 25
  • Thank you so much for your suggestion. I get an error with proc.communicate() and it locks up and times out. I was finally able to execute the students python program from my grader program using os.system(command). Now I just need my grader program to input the user prompts for me automatically. – Brad Rydalch Feb 15 '19 at 18:15
0

I don't know if this is what you're looking for but I'll provide you 2 suggestions...

1) with user input This solution can be added to your code and it doesn't matter what the user types into the prompt the input variable will be assigned to a random value of a list:

import random
mylist = [i for i in range(0,20)]

amount1 = input("amount1")
if amount1:
    amount1 = random.sample(mylist, 1)

print(amount1)

2) without user input Simply remove the necessity for the user to input some value during testing and assign a random value out of your list directly without a need for a user input:

import random
mylist = [i for i in range(0,20)]

amount1 = random.sample(mylist, 1)

print(amount1)

3) JSON file? How about if your teacher stores the items and amount data in a json file and the executed script accesses it?

newbiedude
  • 51
  • 1
  • 5