移动计算机后,为什么我的代码不再有效?

时间:2018-04-02 16:41:28

标签: python python-2.7

所以我在python中的另一台计算机上编写了一些代码,然后将该文件通过电子邮件发送到我的新计算机。现在,它不再有效。以下是附件代码:

from Tkinter import *
from PIL import *
from random import *

w,h = 1200,1200
root = Tk()
canvas = Canvas(root, width=w, height=h, bg='white')
canvas.pack()
colors = ['red', 'green', 'yellow', 'blue', 'orange']
dict = {}
fontsizes = open('lab23.txt', 'r').read().split(" \n")
randY = 0
randX = 0
randColor = 0
print fontsizes
i,k = 0,0
while i < len(fontsizes)-1:
   key = fontsizes[i]
   i += 1
   val = int(fontsizes[i])
   i += 1
   dict[key] = val
print len(fontsizes)
while k < len(fontsizes)-1:
   if len(fontsizes[k]) > 2:
      randY = randint(100, 1100)
      randX = randint(100, 1100)
      randColor = randint(0, 4)
      f = ('Times', dict[fontsizes[k]]) 
      canvas.create_text(randX, randY, text = fontsizes[k], font = f, fill = colors[randColor])
   k +=1
def space(evt):
   k = 0
   canvas.delete('all')
   while k < len(fontsizes)-1:
      if len(fontsizes[k]) > 2:
         randY = randint(100, 1100)
         randX = randint(100, 1100)
         randColor = randint(0, 4)
         f = ('Times', dict[fontsizes[k]]) 
         canvas.create_text(randX, randY, text = fontsizes[k], font = f, fill = colors[randColor])
      k +=1
root.bind('<space>',space)

root.mainloop()

当我运行代码时,收到错误消息:

File "Lab24.py", line 19
    print fontsizes
                  ^
SyntaxError: invalid syntax

有什么问题?

1 个答案:

答案 0 :(得分:1)

第一台计算机可能正在运行python。第二台计算机可能正在运行python3(我相信)要求语法为print(fontsizes)

在您的计划的第19行,您使用print fontsizes

这需要在您打印的所有地方进行更改。我建议使用parathese&#34;()&#34;因为(我相信)它适用于所有版本的python。