How can I take a tuple as input using raw_input in python?

I'm trying to get involved. But this is not the correct syntax.

a, b, c = (int(x) for x in raw_input().strip(' '))

My idea is to take multiple values ​​from one line, which has integers separated by a space. How can i do this?

+4
source share
2 answers

You were very close. He splitdoes not strip:

a, b, c = (int(x) for x in raw_input().split())

It takes exactly 3 integers, nothing more. If you want to take an arbitrary amount into a tuple, try this instead:

tup = tuple(int(x) for x in raw_input().split())
+8
source

I assume that you need a split function, not a strip function. This will return an array that you can iterate over. The strip function will simply remove the start and end characters (in your spaces).

This will throw an exception if the user does not enter data in the correct format.

+2
source

Source: https://habr.com/ru/post/1692801/


All Articles