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()
所以这个程序应该打开一个列表,然后让乌龟在列表上执行命令。如果这条线读起来,那么乌龟把笔放起来。如果该行读取向下,则乌龟放下笔。如果这条线是一对数字,那么乌龟会进入这些坐标。以下是文件“mystery.txt”的内容:Python Invalid Int()Error
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语句中的坐标,则会输出正确的坐标)。 那么有什么帮助?关于如何解决这个问题?
您是否尝试过你的代码调试步骤,检查值? – litelite
仔细看看'outFileContent'中的内容以及您的代码期望的内容...... – AlG