Several are the forms we have in the Python for capture various values from a single input().
Well, to catch all 3 values to use in your code we can use the following command line:
a, b, c = [int(x) for x in input('Digite os valores de "A", "B" e "C": ').split()]
Note that when we use this command line, the entered values are stored in a list, in which the variable a
will receive the first index in the list, the variable b
will receive the second index of the list and the variable c
will receive the third index of the list.
With this command line your code would look like this:
a, b, c = [int(x) for x in input('Digite os valores de "A", "B" e "C": ').split()]
if (a < b) and (a > c):
print("O vice campeão é: ", a)
elif (a > b) and (a < c):
print("O vice campeão é: ", a)
if (b < a) and (b > c):
print("O vice campeão é: ", b)
elif (b > a) and (b < c):
print("O vice campeão é: ", b)
if (c < b) and (c > a):
print("O vice campeão é: ", c)
elif (c > b) and (c < a):
print("O vice campeão é: ", c)
Note that when we execute this code we receive the following message: Digite os valores de "A", "B" e "C":
. Right now we must type all values of A, B and C, in same line, separated for a single space and press enter
. Then the code will perform the comparison operations and show us the result.
Now, if you are also interested in greatly reducing the number of lines in this code, you can use the following code:
valores = sorted([int(x) for x in input('Valores os valores de "A", "B" e "C": ').split()], reverse=True)
maior = max(valores)
for c in valores:
if c < maior:
print(f'O vice campeão tem: {c} pontos')
break
Note that when we execute this second code we receive the same message from the previous code. At this point we must enter the values as we typed in the previous code and press enter
. Then the code will store all the input values inside the list valores
ordained decrescente
. Subsequently, the code will calculate the maior
list value valores
. Then the block for
will traverse the elements of the list valores
and with the help of the block if
will be checked if the value temporarily stored in the variable c
is minor that the variable maior
. If the verification is negative, the for
end such iteration and execute the next. If the verification is positive, the print()
will display the vice champion and, with the help of break
, will end the execution of the block for
, thus producing the general closure of the.
Observing:
Another way to capture various values from a single input is:
valores = list(map(int, input().split()))
Thank you very much, but I still have another doubt: has how to use this same method with vertical lists?
– Gustavo Matias
@Gustavomatias as well as vertical lists?
– Pedro von Hertwig Batista
It is difficult to explain here but instead of typing " 10 9 8 " on each other’s side, for example, if I type these numbers one under the other? The program ( modified the way you said ) would work the same way?
– Gustavo Matias
@Gustavomatias I propose that you publish another question, because this doubt by the is clarified and you must accept the answer as such. The new doubt has no relation to this
– Miguel