Python语句打印错误

Python语句打印错误,python,if-statement,while-loop,Python,If Statement,While Loop,我正在写一个程序,在输入一个空行之前,应该读入输入行。如果该行以Simon开头,则应打印出该行的其余部分。不以Simon says开头的行应该被忽略。因此,我无法编写程序,因为它需要这样输出: Enter: jump Enter: Simon says shout loudly shout loudly Enter: simon would like you to eat a frog Enter: Simon says clap your hands clap your hands Enter

我正在写一个程序,在输入一个空行之前,应该读入输入行。如果该行以Simon开头,则应打印出该行的其余部分。不以Simon says开头的行应该被忽略。因此,我无法编写程序,因为它需要这样输出:

Enter: jump
Enter: Simon says shout loudly
shout loudly
Enter: simon would like you to eat a frog
Enter: Simon says clap your hands
clap your hands
Enter:
我正在编写的代码是:

word = raw_input("Enter: ")
i = ""
while word != i:
    if 'Simon says' in word:
        print word 
    word = raw_input("Enter: ")
在伪代码中:

forever (while True) do the following:
  input a sentence
  if its length is 0: break
  else if it starts with 'Simon says':
     print sentence from the n-th character (sentence[n:]), 
     where n is the length of the string 'Simon says'
在伪代码中:

forever (while True) do the following:
  input a sentence
  if its length is 0: break
  else if it starts with 'Simon says':
     print sentence from the n-th character (sentence[n:]), 
     where n is the length of the string 'Simon says'

看起来您就快到了,您只需要从输出中删除“Simon says”:

print word.replace('Simon says', '')

看起来您就快到了,您只需要从输出中删除“Simon says”:

print word.replace('Simon says', '')

您的代码有两个问题:首先,您的
if
-条件将微妙地做错误的事情-例如

>>> 'hello, simon'.startswith('simon')
False
>>> 'simon' in 'hello, simon'
True
in
测试子字符串是否位于字符串中的任何位置。为了测试它是否正处于起始位置,Python提供了一个名为
startswith
的函数:

>>> 'simon'.startswith('s')
True
您唯一的另一个问题是,当前,您将打印出整个输入字符串,包括要删除的“Simon says”。删除它的最简单方法是使用
str.replace

>>> 'simon says'.replace('simon', 'fred')
'fred says'
用空字符串(
'
)替换将有效地删除子字符串。但这又出现了同样的问题-它将替换字符串中的任何位置:

>>> 'simon says lets play simon says'.replace('simon says', '')
' lets play '
但是您可以告诉它最多只替换一个,因为您已经知道字符串以“Simon says”开头,所以您知道这将是开头的一个:

>>> 'simon says lets play simon says'.replace('simon says', '', 1)
' lets play simon says'
您也可以使用字符串切片-
'fred'[2://code>请求字符串从
'fred'
的第二个字符后开始(因此,从'e'),一直到结尾:

>>> 'fred'[2:]
'ed'
“西蒙说”有10个字母,所以:
word[10:
将是
word
之后的所有内容。但是,如果您错误计算了字母的数量,这很容易导致细微的错误-为了避免这种情况,您可以让Python为您做这件事,如:

word[len('Simon says'):]

您的代码有两个问题:首先,您的
if
-条件将微妙地做错误的事情-例如

>>> 'hello, simon'.startswith('simon')
False
>>> 'simon' in 'hello, simon'
True
in
测试子字符串是否位于字符串中的任何位置。为了测试它是否正处于起始位置,Python提供了一个名为
startswith
的函数:

>>> 'simon'.startswith('s')
True
您唯一的另一个问题是,当前,您将打印出整个输入字符串,包括要删除的“Simon says”。删除它的最简单方法是使用
str.replace

>>> 'simon says'.replace('simon', 'fred')
'fred says'
用空字符串(
'
)替换将有效地删除子字符串。但这又出现了同样的问题-它将替换字符串中的任何位置:

>>> 'simon says lets play simon says'.replace('simon says', '')
' lets play '
但是您可以告诉它最多只替换一个,因为您已经知道字符串以“Simon says”开头,所以您知道这将是开头的一个:

>>> 'simon says lets play simon says'.replace('simon says', '', 1)
' lets play simon says'
您也可以使用字符串切片-
'fred'[2://code>请求字符串从
'fred'
的第二个字符后开始(因此,从'e'),一直到结尾:

>>> 'fred'[2:]
'ed'
“西蒙说”有10个字母,所以:
word[10:
将是
word
之后的所有内容。但是,如果您错误计算了字母的数量,这很容易导致细微的错误-为了避免这种情况,您可以让Python为您做这件事,如:

word[len('Simon says'):]

Daniel房间里有个空间output@rocker789因为
replace
只会准确地替换您告诉它的子字符串,而不是紧跟其后的空格-如果您想去掉空格,也可以将其包含在对
replace
的调用中,如
word.replace('Simon says','')
。Daniel房间里有一块空地output@rocker789因为
replace
只会准确地替换您告诉它的子字符串,而不是紧跟其后的空格-如果您想去掉空格,也可以在调用
replace
时包含它,如在
word.replace('Simon says','')
中。