fossunited / monschool-website

Repository of all courses on mon.school
26 stars 10 forks source link

Support for providing input to programs #54

Open anandology opened 2 years ago

anandology commented 2 years ago

Some examples in mypy-primer uses input and that doesn't work currently.

Proposal

Make it possible to specify stdin and run the program with that as stdin.

Suggested format is:

<span id="data-greet" data-stdin='"foo\n45"'></span>

```{.python .example .hijack-input #greet}
def greet(name):
    print(f"Hello, {name}!")

if __name__ == '__main__':
    name = input("Enter your name: ")
    age = input("Enter your age: ")

    greet(name)
    print(f"You will be {age + 1} years old next year.")
```

Please not that the input is specified as JSON.

PoC

inputs = iter(["foo", "45"])

def new_input(prompt):
    print(prompt, end="")
    x = next(inputs)
    print(x)
    return x

input = new_input

def greet(name):
    print(f"Hello, {name}!")

if __name__ == '__main__':
    name = input("Enter your name: ")
    age = input("Enter your age: ")

    greet(name)
    print(f"You will be {age + 1} years old next year.")