3 回答

TA貢獻1831條經驗 獲得超4個贊
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)))
當您在 print 語句中連接字符串時,您遺漏了一個 + 。

TA貢獻1829條經驗 獲得超6個贊
因為您依賴于字符串文字串聯來獲取由字符串分隔的文字。
這僅適用于字符串文字。編譯器無法將字符串連接到由空格分隔的任意表達式,只能連接到文字。
>>> "foo" "bar"
'foobar'
>>> 'foo' frobnicate()
? File "<stdin>", line 1
? ? 'foo' frobnicate()
? ? ? ? ? ? ? ? ? ?^
SyntaxError: invalid syntax
這發生在編譯時,
>>> import dis
>>> dis.dis("'foo' 'bar'")
? 1? ? ? ? ? ?0 LOAD_CONST? ? ? ? ? ? ? ?0 ('foobar')
? ? ? ? ? ? ? 2 RETURN_VALUE
所以它不能依賴運行時結果。
逗號之所以有效,是因為它只是成為 的另一個參數print。
例如
>>> print('hello')
hello
>>> print('hello', 'world')
hello world

TA貢獻1835條經驗 獲得超7個贊
我建議像這樣設置文本格式:
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))
添加回答
舉報