0

Im getting a SyntaxError: invalid syntax when a run this code:

total = int(input("compra total: "))

if total  > 700000: totald = total - total*0.2
elif total > 300000: totald = total - total*0.15
elif total > 150000: totald = total -total*0.10
else: totald = total*1

print("Centro Comercial Unaleño\n" "Compra Más y Gasta Menos\n" "NIT: 899.999.063\n" "Total:$"+str(int(totald)) "En esta compra tu descuento fue $"+str(int(total-totald)))

I realize that the error is not placing a comma or sum symbol in here:

......"Total: $"+str(int**(totald)), "\nEn** esta compra tu descuento fue $"+str(int(total-totald)))

But i dont understand the reason behind having to place any of the two options. Why I cannot just place a space like in the others strings, and what is the objective of any of the two symbols??

Thanks for the help!!

0

3 Answers 3

1
total = int(input("compra total: "))

if total  > 700000: totald = total - total*0.2
elif total > 300000: totald = total - total*0.15
elif total > 150000: totald = total -total*0.10
else: totald = total*1

print("Centro Comercial Unaleño\n" "Compra Más y Gasta Menos\n" "NIT: 899.999.063\n" "Total:$"+str(int(totald)) + "En esta compra tu descuento fue $"+str(int(total-totald)))

You left out a + when you were concatenating your string in the print statement.

Sign up to request clarification or add additional context in comments.

Comments

0

Because you are relying on string-literal concatenation for literals separated by strings. See the documentation.

This only works for string literals. The compiler cannot concatenate a string to an arbitrary expression separated by a space, only a literal.

>>> "foo" "bar"
'foobar'
>>> 'foo' frobnicate()
  File "<stdin>", line 1
    'foo' frobnicate()
                   ^
SyntaxError: invalid syntax

This happens at compile time,

>>> import dis
>>> dis.dis("'foo' 'bar'")
  1           0 LOAD_CONST               0 ('foobar')
              2 RETURN_VALUE

So it cannot rely on runtime results.

The comma works because then it just becomes another argument to print.

e.g.

>>> print('hello')
hello
>>> print('hello', 'world')
hello world

1 Comment

Thanks a lot Juanpa! your explanation was amazing!
0

I would recommend formatting the text like this:

print("Centro Comercial Unaleño\r\nCompra Más y Gasta Menos\r\nNIT: 899.999.063\r\nTotal:${} En esta compra tu descuento fue ${}".format(totald, totald))

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.