2017-08-22 37 views
0
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语句中的坐标,则会输出正确的坐标)。 那么有什么帮助?关于如何解决这个问题?

+0

您是否尝试过你的代码调试步骤,检查值? – litelite

+0

仔细看看'outFileContent'中的内容以及您的代码期望的内容...... – AlG

回答

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 
# ... 
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' 

很明显,您的错误来自您的while循环的第二个循环后,您将索引增加一。我假设你想增加三个指数?

请注意,您可能必须执行pip install ipdb --user

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