Is there a clean "Pythonic" way to write an input-replacement function in Python that will yield a value from a predetermined list of input values each time it's called?
raw_data = ['8', '2', '1']
c = 0
def input():
global c
c += 1
return raw_data[c - 1]
for _ in range(3):
print(input())
This does and is expected to output:
8
2
1
Intuitively yield seems like it ought to be part of the solution but I cannot wrap my head around how to implement it as an input replacement.
Two ways I've been using for that:
Of course you don't need the
[::-1]if you're ok with writing the list backwards.If you also need the list for something else, you can use the first solution but with the list stored in an extra variable.
Though the normal
input()returns strings, so what I actually usually use for that is like this, where I copy&paste a given input data block: