在python的一行中读取两个单独的值 [英] Reading two separate values in one line in python
本文介绍了在python的一行中读取两个单独的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要你的帮助.到目前为止,这是我的程序
I need your help. This is my program so far
import turtle
turtle.showturtle()
def turtle_interface():
while True :
n = 0
instructions = input().split()
i = instructions[0]
if len(instructions) > 1:
n = int(instructions[1])
if i == 'forward' :
turtle.forward(n)
elif i == 'backward' :
turtle.backward(n)
elif i == 'left' :
turtle.left(n)
elif i == 'right' :
turtle.right(n)
elif i == 'quit' :
break
elif i == 'new' :
turtle.reset()
else :
continue
print('Control the turtle!')
turtle_interface()
如您所见,当字符串后面没有 [n] 时,它将被忽略.我该如何解决这个问题?
As you can see, when the string has no [n] after it, it's being ignored. How can I fix this?
推荐答案
我认为是因为 if len(instructions) >1:
测试.如果字符串后面没有[n],那么就只有一条指令,长度不会大于1.
I think it is because of the if len(instructions) > 1:
test. If the string has no [n] after it, then there will only be one instruction, and the length will not be greater than 1.
你应该尝试这样的事情:
You should try something like this:
def turtle_interface():
while True :
n = 0
instructions = input().split()
i = instructions[0]
if len(instructions) > 1:
n = int(instructions[1])
if i == 'forward' :
turtle.forward(n)
elif i == 'backward' :
turtle.backward(n)
elif i == 'left' :
turtle.left(n)
elif i == 'right' :
turtle.right(n)
elif i == 'new' :
turtle.reset()
elif i == 'quit' :
break
注意 if i == 'new'
行的缩进和位置.
Note the indentation and placement of the line for if i == 'new'
.
这篇关于在python的一行中读取两个单独的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文