import turtle
index = 0 #to traverse through list
turtle.setup(800,600) # Change the width of the drawing to 800px and the
height to 600px.
wn = turtle.Screen()
sammy = turtle.Turtle()
inFile = open('mystery.txt','r')
outFile=inFile.read()
outFileContent = outFile.split ()
while index != (len(outFileContent)): #for item in the list
if str(outFileContent [index]) == "UP": #if the current index goes up, pen up
sammy.penup()
elif str(outFileContent [index]) == "DOWN": #else if its down, pen down
sammy.pendown ()
elif outFileContent[index].isalpha () == False : #if its a number, go to
those coordinates
sammy.goto (int(outFileContent[index]),int(outFileContent[index+1])) #convert from str to int
index+=1 #goes to next value in list
print ((int(outFileContent [index]), int(outFileContent[index+1]))) #make sure its printing the right coordinates
inFile.close()
wn.mainloop ()
所以这个程序应该打开一个列表,然后让乌龟在列表上执行命令。如果该行读取UP,则乌龟将笔放上。如果该行显示为DOWN,则乌龟将笔放下。如果该行是一对数字,则乌龟将转到那些坐标。以下是文件" mystery.txt"的内容:
UP
-218 185
DOWN
但是,当我运行程序时,它会输出:
Traceback (most recent call last):
File "C:\Users\Yariel\Google
Drive\Coding\Python\Scripts\Files\turtle_file_mystery_shape.py", line 23, in <module>
print ((int(outFileContent [index]), int(outFileContent[index+1]))) #make
sure its printing the right coordinates
ValueError: invalid literal for int() with base 10: 'DOWN'
我不知道它为什么会变成一个我从未指定过的整数。 (如果查看print语句中的坐标,则输出正确的坐标)。 那有什么帮助吗?关于如何解决这个问题?
答案 0 :(得分:0)
我强烈建议学习如何使用ipdb。简单地说,只需将行import ipdb; ipdb.set_trace()
放在打印语句之前,然后单步执行n
,然后继续执行c
。
22 import ipdb; ipdb.set_trace()
---> 23 print ((int(outFileContent [index]), int(outFileContent[index+1]))) #make sure its printing the right coordinates
24
ipdb> outFileContent
['UP', '-218', '185', 'DOWN']
ipdb> outFileContent[index]
'-218'
ipdb> outFileContent[index+1]
'185'
ipdb> c
(-218, 185)
21
---> 22 import ipdb; ipdb.set_trace()
23 print ((int(outFileContent [index]), int(outFileContent[index+1]))) #make sure its printing the right coordinates
ipdb> outFileContent[index]
'185'
ipdb> outFileContent[index+1]
'DOWN'
很明显,在将索引增加1之后,您的错误来自while循环的第二个循环。我假设你想把索引增加三个?
请注意,您可能需要pip install ipdb --user
。
答案 1 :(得分:0)
坐标是一种特殊的输入案例。您需要将index
增加1
以容纳第二个int
。
# ...
elif outFileContent[index].isalpha () == False : #if its a number, go to those coordinates
sammy.goto (int(outFileContent[index]),int(outFileContent[index+1])) #convert from str to int
# move print to here; indexing changed
print ((int(outFileContent [index]), int(outFileContent[index+1])))
# additional increment to index to accommodate second int
index += 1
index+=1 #goes to next value in list
# ...
答案 2 :(得分:0)
我建议您重新考虑一下,而不是修补您的代码。下面的返工将逐行处理神秘文件,而不是一次性处理,并且希望您更容易扩充和调试:
import sys
from turtle import Turtle, Screen
screen = Screen()
# Set width of the drawing to 800px and height to 600px
screen.setup(800, 600)
sammy = Turtle()
commands = {'UP': sammy.penup, 'DOWN': sammy.pendown}
with open('mystery.txt') as inFile:
for line in inFile:
content = line.rstrip().split()
if len(content) == 1 and content[0] in commands:
commands[content[0]]() # eg. UP and DOWN
elif len(content) == 2 and not content[0].isalpha() and not content[1].isalpha():
# seem to be numbers, convert from str to int and go to those coordinates
sammy.goto(int(content[0]), int(content[1]))
else:
print("Invalid input:", content, file=sys.stderr)
screen.mainloop()