+ 1

Is it possible to have a input() be able to take a string or integer.

How would I be able to have an input be able to have a string or integer entered without changing the statement type (str/int/etc).

10th Oct 2024, 2:04 AM
SolekProgrammer
SolekProgrammer - avatar
2 Answers
+ 5
input()n will always bring in a string because the function doesn't know what someone might type in. You just have to convert it to whatever you need. This gives you control over error handling / exception handling.
10th Oct 2024, 2:51 AM
Jerry Hobby
Jerry Hobby - avatar
+ 2
You would have to overwrite the function with your own implementation that can return an integer or other misc appropriate data type. Python's built in function assumes string, always.
10th Oct 2024, 5:06 AM
Rrestoring faith
Rrestoring faith - avatar