Two values from one input in python?

前端 未结 18 2253
小鲜肉
小鲜肉 2020-11-28 03:46

This is somewhat of a simple question and I hate to ask it here, but I can\'t seem the find the answer anywhere else: is it possible to get multiple values from the user in

18条回答
  •  时光说笑
    2020-11-28 03:56

    In Python 3, raw_input() was renamed to input().

    input([prompt])
    

    If the prompt argument is present, it is written to standard output without a trailing newline. The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that. When EOF is read, EOFError is raised.

    So you can do this way

    x, y = input('Enter two numbers separating by a space: ').split();
    print(int(x) + int(y))
    

    If you do not put two numbers using a space you would get a ValueError exception. So that is good to go.

    N.B. If you need old behavior of input(), use eval(input())

提交回复
热议问题