Python 我在说字符串return'';但是我有一个0。它不是穿过绳子吗?
这是我的密码:Python 我在说字符串return'';但是我有一个0。它不是穿过绳子吗?,python,string,indexing,numbers,python-turtle,Python,String,Indexing,Numbers,Python Turtle,这是我的密码: import turtle def first_index(s): for i in range(len(s)): if not (s[i].isdigit()): return i return None def go_to(s): if s[0] == 'G': comma_ind = first_index(s) first_arg
import turtle
def first_index(s):
for i in range(len(s)):
if not (s[i].isdigit()):
return i
return None
def go_to(s):
if s[0] == 'G':
comma_ind = first_index(s)
first_arg = int(s[1:comma_ind])
second_arg = int(s[comma_ind + 1:comma_ind + first_index(s[comma_ind + 1:])])
turtle.goto(first_arg, second_arg)
go_to('G10,10')
有人知道为什么会发生这种错误吗?非常感谢您的帮助,谢谢 两个问题:
- 如果未找到非数字字符,则第一个索引应返回最后一个索引
- 获取第二个参数时,将向index函数发送一个部分字符串,因此需要添加所传递字符串的起始索引
import turtle
#This gets the
def first_index(s):
for i in range(1,len(s)):
if not (s[i].isdigit()):
return i
return len(s)-1 # reached end of string
def go_to(s):
if s[0] == 'G':
comma_ind = first_index(s)
first_arg = int(s[1:comma_ind])
second_arg = int(s[comma_ind + 1:comma_ind + first_index(s[comma_ind + 1:])+comma_ind]) # add comma index to returned index
turtle.goto(first_arg, second_arg)
go_to('G99,99')
input()
输出
看起来您的
返回None
没有缩进到正确的深度。另外,如果您没有从函数返回任何内容,默认情况下它将返回None。所以您可以删除返回None
行。