0

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

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' 
4

1 回答 1

0

我相信您要查找的内容称为切片而不是减法,特别是在第 3行中。

当我在空闲状态下运行您在此处粘贴的代码时,它工作正常,这是因为 else 语句应该处理来自的任何类型错误用户输入。
但是,您可以阅读有关字符串以及如何切片的更多信息。试试这个:http ://docs.python.org/release/1.5.1p1/tut/strings.html

于 2013-05-19T02:20:35.523 回答