+ 2

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
6 odpowiedzi
+ 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
0
U can a = str(input(" hdhdvd"))
11th Oct 2024, 8:15 PM
Marcoxian
Marcoxian - avatar
0
I am shocked honestly.. OP didn’t ask to make the input() function specifically work like that. If you lack in knowledge at least don’t speak and lie to people. There is a way, simple and efficient. You def a func that takes input like the built in input() func and you check with it. The output can be adjusted to suit your needs. You call it and it takes an input like the built in input(). https://sololearn.com/compiler-playground/cYkDw241vySU/?ref=app I wrote this for you, check it. You write it once, after that just call the function and you have both the type and the value set to the proper data type, ready to use.
12th Oct 2024, 1:56 AM
Uwltoamym
Uwltoamym - avatar
0
Hi
29th Oct 2024, 10:27 PM
LF Niloy
LF Niloy - avatar
0
45 web page create
29th Oct 2024, 10:28 PM
LF Niloy
LF Niloy - avatar