为什么这段代码不起作用?我只是想切片。(在python中)

时间:2013-05-19 00:42:04

标签: python slice

当我输入此代码时,切片的工作方式出错...它不会让我输入它,它总体上不起作用。

verb_1 = raw_input("Enter a word")
verb_2 = verb_1.lower()
verb_2_ending = verb_2[-2:]
if verb_2_ending == "ar": 
    ar_verb = raw_input("you've chosen an AR verb. What tense would you like")
elif verb_2_ending == "er":
    er_verb = raw_input("you've chosen an ER verb. What tense would you like")
else :
    ir_verb = raw_input("you've chosen an IR verb. What tense would you like")

if ar_verb == "subjunctive":
    subject_1 = raw_input("What is the subject")
elif  ar_verb == "present":
    subject_2 = raw_input("What is the subject")
else :
    subject_3 = raw_input("What is the subject")

产生错误:

Enter a word : hablar 
Traceback (most recent call last): 
File "<stdin>", line 4, in <module> 
  TypeError: unsupported operand type(s) for -: 'str' and 'str' 

1 个答案:

答案 0 :(得分:0)

我相信你所寻找的东西叫做切片而不是减法,特别是第3行。

当我运行你在这里闲置的代码时这是正常的,这是因为else语句应该处理来自用户输入的任何类型错误。
然而,您可以阅读有关字符串和切片的更多信息。试试这个:http://docs.python.org/release/1.5.1p1/tut/strings.html