Simple Python interactive input/output tests?

Forgive my ignorance, but could someone point to the absolute simplest way of using the GitHub Classroom tests for Python input/output in interactive programs?
I have had a look at the autograding instructions and the templates post here, but I’m hoping to do it very simply through the Classroom UI instead of running pytest etc. Is this possible?

E.g. Say I want students to write a program that asks the user for a string and prints that string with an exclamation mark at the end:

    text = input("Enter: ")
    sentence_text = text[0].upper() + text[1:] + '.'
    print(sentence_text)

Test inputs:

Thingo.
I am happy.
I am sad.

Expected outputs:

thingo!
I'm happy!!

I’ve tried adding a test in the classroom interface and entering these inputs and outputs, but the test fails with NameError: name 'thingo' is not defined, which make me think I’m doing it incorrectly.

Thanks in advance!

OK, getting closer.
I’ve seen that error before when students accidentally use Python 2 instead of Python 3.
I changed my run command from python to python3 so now it runs but my tests don’t pass, with:

::error::The output for test Sentence case did not match%0AExpected:%0AThingo.%0AI am happy.%0AI am sad.%0AActual:%0AEnter: Thingo%0D.

In @jeffrafter 's video:

he does not need to include the prompts that his Java program prints - only the user inputs and outputs.
Is it different for Python, or am I missing something?

(1) Set your Repl.it run command and language as below:

(2) Set your test like this:

(3) Make sure your students write their code in the file main.py

(4) Success!

Code:

text = input("Enter: ")
sentence_text = (text[0].lower()+text[1:-1]).strip('.')+'!'
print(sentence_text)

Thank you so much for the prompt reply!

The only consequential difference was that I had multiple inputs and outputs (as in my post).
I had (mis)understood that this was possible, especially since the UI says “inputs” (plural).

When I changed it to just “thingo” - no other cases, it worked! :slight_smile:

Do I need to have only single runs/input in each test?
If so, is the expectation that you should duplicate each test with slightly different input/output each time?
If that’s the case, can the UI be changed to “input” (singular) to make this clearer?
And/or perhaps you could update the UI to make it easier to have one “test” with multiple pairs of input and output.

Thought: perhaps “inputs” is plural because it allows for multiple input() in a single run of a program rather than multiple cases…
I tried this:

Code:

name = input("Name: ")
age = int(input("Age: "))
print(f"Hello {name}. You are {age} years old.")

(which runs correctly)

But the test failed with:

::error::The output for test Second with two different did not match%0AExpected:%0AHello Lindsay. You are 43 years old.%0AActual:%0AName: Age: Hello Lindsay%0D. You are 43 years old.

This seems a bit odd since the single-input case doesn’t show that the input prompt is part of the actual output, but this does.

I’d appreciate your further guidance on this, thank you.

On that test, when you see %OD it’s URL encoding for a “return”, not like a regular linefeed (%OA).

So if you go into your repo’s .github/classroom/autograding.json file, you can tweak the test a bit, and it’ll work fine. The differences between linefeed ( \r\n ) and return ( \r ) get annoying.

{
  "tests": [
    {
      "name": "Second with two different",
      "setup": "",
      "run": "python3 another.py",
      "input": "Lindsay\r\n43",
      "output": "\r\nName: Age: Hello Lindsay\r. You are 43 years old. ",
      "comparison": "included",
      "timeout": 10,
      "points": null
    }
  ]
}

Anyways, main takeaway here:
%OD = \r
%OA = \r\n

With that update on line 8 of your json file, the test works perfectly.

Oh and you can have multiple inputs in a single test.

I personally found tweaking the built-in autograding was much more annoying than just making my own pytests.

Thanks again @RuizTheRuler :+1:
When you say:

go into your repo’s .github/classroom/autograding.json file…

which repo is this exactly? I have a student repo, but obviously I don’t want to change every student’s repo; I have a starter code template repo that does not contain this file/folder, and I have a classroom assignment but can’t see an actual repo for it. Where do I find this exactly?

I did try your suggestion with my test student repo, and indeed it passed :slight_smile:
But, it doesn’t seem like a sustainable/extensible way to make tests.
If the UI doesn’t create the tests in a usable way - and doesn’t tell me about how to fix/change them to make it work - it seems incomplete (but currently implies it should work out of the box).
Am I missing something that can be done via the UI?

When you say:

Oh and you can have multiple inputs in a single test.

Are you referring to my first or second issue?
First = (my first test) multiple versions (pairs) of inputs/outputs for the same
Second = (my second test) one run with multiple input() function calls

If it’s the second, then I see this as above (but requiring the non-UI fix).
If it’s the first, I’d love it if you would please explain further.

Again, thank you so much for your time! :pray:
I also hope that my experience helps GitHub to update either the UI and/or the documentation to explain this further.

1 Like

Hey!

I think I’m a little confused as to what exactly you’re asking. I don’t think I understand your workflow for creating student assignments.

I think, possibly, you are creating “blank” assignments. What I mean by that, is assignments not created from a template repository? Is this correct?

If so, that’s going to create more work for you in the long run. Like, a lot more work. You set up template repo’s for assignments, include all the tests and student directions in them, then use them semester after semester without ever having to redo anything. That’s the beauty of this whole thing. It is efficiency to the max. It is one-and-done. Never have to repeat yourself. never have to duplicate work. It is creating this collection of assignments that is ready to go, you just push a button for the next semester. Tried, tested, true.

I would love to help you out. I know it is so daunting setting things up if you don’t have someone to help you, to show you. Let me know if I can be that person! I’ve been in your position and it’s overwhelming.

© 2017 GitHub, Inc.
with by
GitHub Education