I have some source that fails. input = input('What shall we send...?') is the command…
But…
If I do input = int(input('What shall we send...?')), I get only integers as available input. I can do the same with float numbers as well.
If I do input = str(input('What shall we send...?')), I get only strings as available input.
Is there some particular way to call all ASCII or Unicode characters for testing the source or am I limited to the separate lines of code for int and str?
try:
while True:
input = input('What shall we send...?')
for letter in input:
for symbol in CODE[letter.upper()]:
if symbol == '-':
dash()
elif symbol == '.':
dot()
else:
sleep(0.5)
sleep(0.5)
except KeyboardInterrupt:
Here is the Morse Code gist of the idea I am attempting where CODE is a dictionary.
If I’m interpreting what you’re doing correctly, input(...) returns a string, and int(input(...)) converts that string to a integer. It will raise an error if it can’t. str(input(...)) is probably equivalent to plain input(...).
PS: input=input(...) is a bad pattern. Prefer input_ = input(...) or my_input = input(...) or something more meaningful if possible.
Your code is probably crashing because you’re redefining input, and then trying to use it as a function when it no longer is a function?
Regarding the string and float and int inputs, the classic pattern is
var = input("...")
try:
var = int(var)
except ValueError:
try:
var = float(var)
except ValueError:
pass
ie you try to convert the string to an int, and if that raises an error you try to convert it to float, and otherwise don’t convert it.
More modern python we have
import contextlib
var = "123.3"
try:
var = int(var)
except ValueError:
with contextlib.suppress(ValueError):
var = float(var)
I will test and report back. Thank you kind person.
Seth
P.S. The source from my dictionary calls characters only (strings). So, when I type any character as input, the return is an ongoing I/O on the console while if I only type integers, I get the error:
Scratch whatever I said...
Everything works as is now… input = input was an error…