How do I receive input in python?



  • Python input function.

    input(prompt=None, /)
        Read a string from standard input.  The trailing newline is stripped.
        
        The prompt string, if given, is printed to standard output without a
        trailing newline before reading input.
        
        If the user hits EOF (*nix: Ctrl-D, Windows: Ctrl-Z+Return), raise EOFError.
        On *nix systems, readline is used if available.
    

    Demo of python input function along with print & int functions

    miles_walked = input('How many miles did you walk? ')
    kilometres_walked = int(miles_walked) * 1.6
    print('In Kilometres you walked', kilometres_walked, 'km')
    

    Output:

    $ ./main.py
    How many miles did you walk? 
    20
    In Kilometres you walked 32.0 km
    

    The int function could also be used around the input function, as below As with all coding, use what is most readable.

    miles_walked = int(input('How many miles did you walk? '))
    kilometres_walked = (miles_walked) * 1.6
    print('In Kilometres you walked', kilometres_walked, 'km')
    

Log in to reply
 

© Lightnetics 2024