How do I put the value of a variable in a statement of another variable?

Asked

Viewed 741 times

1

NomeDoProduto1 = str(input("Qual o seu primeiro produto? "))
NomeDoProduto2 = str(input("Qual o seu segundo produto? "))
NomeDoProduto3 = str(input("Qual o seu terceiro produto? "))
PrecoProduto1 = float(input("Digite o preço do")) print(NomeDoProduto1)

In the code above I want that in line 4 in the print the message appears type the price of:(and here appears the name of the first product). Like if on line 1 I said my product name is sugar then I want q on line 4 to appear (enter sugar price).

disregard line 2 and 3 which is my code. I was trying to do so but the syntax error. I know it is a basic error tbm.

2 answers

1


You can do this in multiple ways.

1) Concatenating the strings

As you noticed can only concatenate the strings:

PrecoProduto1 = float(input("Digite o preço do:" + NomeDoProduto1))

2) Multiple calls from print no line break

You can make different calls to the function print, but set that you do not want to add line break at the end, which is added by default in Python.

print('Digite o preço do: ', end='')
print(NomeDoProduto1, end='')
PrecoProduto1 = float(input())

I do not recommend... prefer option 3 always.

3) Interpolation of values

As of version 3.6 of Python there are f-strings that allow you to interpolate strings:

PrecoProduto1 = float(input(f"Digite o preço do {NomeDoProduto1}: "))

Note the prefix f in string (therefore the name f-string).

More information:

0

I don’t know if I should answer my questions, but searching here I saw that just put a "+" after closing quotes and the name of the variable. type:

PrecoProduto1 = float(input("Digite o preço do:" + NomeDoProduto1))

Browser other questions tagged

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