• 0 Posts
  • 10 Comments
Joined 8 months ago
cake
Cake day: March 15th, 2024

help-circle





  • the signature for the input function (that’s what it’s called instead of command) is

    def input(__prompt: Any = ...) -> str
    

    which means it’s always going to return a string.

    So it starts off as a string, then becomes whatever is typed in

    there’s no real way for something to do that automatically without a much more robust setup.

    this snippet proves that

    test_int = input('enter integer:')
    print(type(test_int))
    test_float = input('enter float:')
    print(type(test_float))
    test_str = input('enter string:')
    print(type(test_str))
    
    >> <class 'str'>
    >> <class 'str'>
    >> <class 'str'>
    

    it is the responsibility of your program to validate and do whatever you want with the result, and part of that can include casting it to a different type.