2 commands in the same python line

Asked

Viewed 1,911 times

0

I wonder how I can fix this

input('Acabei de encontrar a receita ')
print('voce vai precisar dos seguindos ingredientes')
num1 = int('6')
print(num1)
print('Kg de Farinha')
num2 = int('3')
print(num2)
input('Unidades de ovos')
print('')
print('ou add no carrinho')
print('Ok, vou calcular')
print('Voce vai facar com')
calculo = farinha - num1
calculo2 = ovos - num2
print('')
print('Voce vai ficar com {} de farinha e {} de ovos'.format(calculo, calculo2))

As you can see, the flour number is separated from the phrase flour because I need to use the flour number to subtract with what I have, but if I put 2 kg of flour, for example, it will not recognize as number and will give error. I wanted to know how I can use 2 different commands on the same example line:

num1 = int(print('3')    print('de farinha') 
  • 1

    I don’t understand your doubt.

  • What you expect from the user when you do the first input? Where the objects are defined farinha and ovos? What you expect to receive from the user in the second input? There’s a lot of stuff in your code that didn’t make sense, so there’s no way to answer it clearly. You want to read from the user the value "2 kg flour" or need to display this data on the same line?

2 answers

1

In your own code you have the answer.

But giving the solution:

You can put the numbers together with the letters using print() so that you indicate the place of a number with {} and then "fill" with the format() function, as you did on the last line.

Now, about using more than one command on the same line, use the ; when finishing a command.

print('voce vai precisar')
num1 = int('6'); print('{} kg de farinha'.format(num1))
calculo = 1 + num1; print(calculo)

Code working with example

1

There are many ways to do it. I think the coolest and most organized is to declare the variables at the beginning, including declaring them in the same line, just as you want to do in print.

If I understood well your calculation in the example you gave was missing thing. I corrected it according to my understanding, so check to see if this is what you wanted. I also put the calculations inside the print itself so you can see that this is possible. If you want to do it the way you were doing before.

num1, num2 = 6, 3
farinha, ovos = 12, 9

print('Acabei de encontrar a receita ')
print('\nvoce vai precisar dos seguindos ingredientes:')
print('{} Kg de Farinha' .format(num1))
print('{} Unidades de ovos' .format(num2))
print('')
print('Vou add no carrinho')
print('Ok, vou calcular')
print('\n Após fazer a receita você vai ficar com {} kg de farinha e {} ovos'.format(farinha - num1, ovos - num2))

Note: In Python you do not need to explicitly say that the variable is an integer when you are declaring it directly in the code. He interprets this on his own and makes the assignment correct. Let’s say the type of the variable when asking the user to enter it via input, for example:

numero_1 = int(input('Entre com um valor inteiro': )

Browser other questions tagged

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