How to assign 3 values to 3 variables in only one python input line?

Asked

Viewed 2,911 times

0

# Esse é um programa que você irá digitar 3 pontuações e ele irá te informar o vice campeão (ou segundo lugar)  


# Existe algum método de atribuir o valor de a, b e c em apenas uma linha na entrada?  
# Eu escrevo na entrada por exemplo: 10 11 9
# E o programa atribuirá 10 a variável a, 11 a b, e 9 a c 
# Como faço isso?

# No caso, queria substituir os próximos 3 comentários

#a= int (input ("Digite a pontuação: "))
#b= int (input ("Digite a pontuação: "))
#c= int (input ("Digite a pontuação: "))

print()

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)

3 answers

5


A first step would be to take the scores and call the method split to get a list where each element is one of the scores:

pontuacoes = input("Digite as pontuações: ").split()

Next, we have to turn these scores that at the moment are strings into integers. We can do this with a list comprehension:

pontuacoes_int = [int(p) for p in pontuacoes]

Now, pontuacoes_int is a list of scores as integer values. If we want to assign these values to separate variables, we can do

a, b, c = pontuacoes_int

Another way to do this without list comprehension could be like this:

pontuacoes = input("Digite as pontuações: ").split()
a, b, c = int(pontuacoes[0]), int(pontuacoes[1]), int(pontuacoes[2])

This method also works, but it’s interesting to learn how to use list understandings (or map) because then your code works regardless of the number of elements that need to be transformed into integers.

  • Thank you very much, but I still have another doubt: has how to use this same method with vertical lists?

  • @Gustavomatias as well as vertical lists?

  • 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?

  • @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

4

In Python the assignment of values to variables is very cool.

Take an example:

idade, nome, cidade = 21, 'Paulo', 'São Paulo'

In the example above, in a single line we assign 3 values to 3 variables. Just separate the values and variables using a comma.

Other simple examples:

nome, idade, cidade = input('Digite seu Nome: '), int(input('Digite sua idade: ')), input('Digite sua Cidade: ')

1

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()))

Browser other questions tagged

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