iT邦幫忙

0

在mblock的python編輯器中,要執行程式時,出現錯誤訊?!syntaxError missing parentheses in call to 'print'

  • 分享至 

  • twitterImage

在mblock的python編輯器中,要執行程式時,出現錯誤訊?!syntaxError missing parentheses in call to 'print'請問如何解決!

要執行的程式是

#!/usr/bin/env python
"""
moonphase.py - Calculate Lunar Phase
Author: Sean B. Palmer, inamidst.com
Cf. http://en.wikipedia.org/wiki/Lunar_phase#Lunar_phase_calculation
"""

import math, decimal, datetime
dec = decimal.Decimal

def position(now=None):
if now is None:
now = datetime.datetime.now()

diff = now - datetime.datetime(2001, 1, 1)
days = dec(diff.days) + (dec(diff.seconds) / dec(86400))
lunations = dec("0.20439731") + (days * dec("0.03386319269"))

return lunations % dec(1)

def phase(pos):
index = (pos * dec(8)) + dec("0.5")
index = math.floor(index)
return {
0: "New Moon",
1: "Waxing Crescent",
2: "First Quarter",
3: "Waxing Gibbous",
4: "Full Moon",
5: "Waning Gibbous",
6: "Last Quarter",
7: "Waning Crescent"
}[int(index) & 7]

def main():
pos = position()
phasename = phase(pos)

roundedpos = round(float(pos), 3)
print "%s (%s)" % (phasename, roundedpos)

if name=="main":
main()

https://ithelp.ithome.com.tw/upload/images/20200727/20058503vODWlAWD6D.png

看更多先前的討論...收起先前的討論...
dragonH iT邦超人 5 級 ‧ 2020-07-27 14:57:50 檢舉
google 一下人家給的error message ...

https://blog.csdn.net/weixin_41100555/article/details/80448483
froce iT邦大師 1 級 ‧ 2020-07-27 15:30:48 檢舉
不要拿python3來跑python2的code...
myubuntu iT邦新手 4 級 ‧ 2020-07-27 15:36:59 檢舉
請問如何看出python2或是python3
有辦法將這段改為python2嗎?
請問如何改為python3的正確輸出?
myubuntu iT邦新手 4 級 ‧ 2020-07-27 15:48:11 檢舉
print(format(phasename))
dragonH iT邦超人 5 級 ‧ 2020-07-27 16:07:57 檢舉
print(f'{phasename} ({roundedpos})')

不知道你是 3.x
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
微甜的酸
iT邦新手 2 級 ‧ 2020-07-27 16:45:59

再python3使用python2的print語句就會這樣

print "%s (%s)" % (phasename, roundedpos)

myubuntu iT邦新手 4 級 ‧ 2020-07-27 17:41:41 檢舉

File "main.py", line 41
print "%s (%s)" % (phasename, roundedpos)
^
IndentationError: unindent does not match any outer indentation level

我要發表回答

立即登入回答