亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Python 中的簡單代碼中不使用逗號的“SyntaxError:無效語法”背后的原因

Python 中的簡單代碼中不使用逗號的“SyntaxError:無效語法”背后的原因

繁星coding 2023-09-05 20:26:14
運行此代碼時,我收到 SyntaxError:無效語法:total = int(input("compra total: "))if total  > 700000: totald = total - total*0.2elif total > 300000: totald = total - total*0.15elif total > 150000: totald = total -total*0.10else: totald = total*1print("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)))我意識到錯誤不是在這里放置逗號或求和符號:......"Total: $"+str(int**(totald)), "\nEn** esta compra tu descuento fue $"+str(int(total-totald)))但我不明白必須放置這兩個選項中任何一個的原因。為什么我不能像其他字符串一樣放置一個空格,這兩個符號的目的是什么?謝謝您的幫助?。?
查看完整描述

3 回答

?
慕容708150

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 語句中連接字符串時,您遺漏了一個 + 。


查看完整回答
反對 回復 2023-09-05
?
肥皂起泡泡

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


查看完整回答
反對 回復 2023-09-05
?
qq_花開花謝_0

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))


查看完整回答
反對 回復 2023-09-05
  • 3 回答
  • 0 關注
  • 184 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號