2 commands on the same line python [closed]

0

I would like to know how I can resolve 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 number of flour is separate from the flour phrase because I need to use the number of flour 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 in the same line example:

num1 = int(print('3')    print('de farinha') 
    
asked by anonymous 07.05.2018 / 22:43

2 answers

1

There are several ways to do it. I think the most legal and organized is to declare the variables at the beginning, even declare them on the same line, just like you want to do in print.

If I understood correctly your calculation in the example you gave was missing something. I corrected it according to my understanding, so check it out if that's 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 have to explicitly say that the variable is an integer when you are declaring it directly in the code. He interprets this by himself and does the correct assignment. Let's say the variable type when prompting the user to enter it via input, for example:

numero_1 = int(input('Entre com um valor inteiro': )
    
08.05.2018 / 03:04
0

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 in" with the format () function as you did in the last line.

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

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

Code working with example

    
07.05.2018 / 22:59