Is it possible to include more than one variable per input in Python?

Asked

Viewed 2,675 times

6

In C I can do it:

printf("Informe 3 numeros');
scanf("%d%d%d", &a,&b,&c);

Can I do a similar process in Python? I did some searches and found that I can do so:

a,b,c = input('informe 3 numeros: ').split("")
print(a,'',b,'',c)

But the following mistake is making:

File "C:/Users/Administrador/Google Drive/Exercicios Python/embaralha 
palavra.py", line 1, in <module>   a,b,c = input('informe 3 numeros:
').split("") ValueError: empty separator 
  • 1

    You are reporting the numbers separated by space, but you are trying to divide the string with "", why? Do the .split(" ") to separate in the blank.

  • Can also do split() without indicating the separator that the default separator is a space and will divide by words as you want.

3 answers

5


It is not possible, the function input() only returns one value always. Even what you are trying to do is not to return 3 separate data, it is returning a value and is breaking it down to 3 right after the input() (the input() has already been executed and brought only one string) with its result, and then you can play on the various variables.

The mistake is that you are trying to break the data with an empty tab, this does not work, would have to for a space:

a, b, c = input('informe 3 numeros: ').split(" ")
print(a, '', b, '', c)

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

But this only works if the person type right, with the correct spaces. So do not do so, do 3 input()s and be happy. Even in C is better so.

And note that in C you already save as an integer (in the form shown), in Python you will still have a text, and if you want number you would have to do the conversion individually in each variable.

2

Valueerror: Empty separator

Instead of . split(""), make . split(" ")

Just remembering that the input returns a string, that is, if you want to use as operands you have to transform to integer or float. And also in this type of input the validation has to be done, because the user can for 3 spaces between each number, there goes the shit...

1

If your intention is to capture multiple values from one input(), you can use one of the command lines listed below:

Example 1:

valores = list(map(int, input('Digite todos os valores: ).split()))

Example 2:

valores = [int(x) for in input('Digite todos os valores: ).split()]

Example 3:

a, b, c = [int(x) for in input('Digite todos os valores: ).split()]

Note that when we run each of the 3 examples listed above, we received the following message: Digite todos os valores: . Right now we must type all the intended values, in same line, separated for a single space and press enter.

In both examples, the values captured by input() are stored in a list.

In the first two examples we can type a quantity undefined of values, that is to say, how many values we want.

In the third example, we can only type the amount of whatever values equal to the number of variables that, perhaps, is the left side of the assignment signal - which in Pytho is represented by the =. In this specific case, we will only be able to type 3 values.

Also, note that to use one of the three forms listed above, we need to identify the guy desired variable. In the examples cited above, I used only the whole type - int. However, we may use other types of variables. To do this, simply replace the type int, by the type desired.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.