简体   繁体   中英

Python script providing input for other python programs

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 [ ]

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 .

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM