2017-04-12 75 views
-2
def consecutiveLetter(str): 
    x=0 
    flag=0 
    while(x<(len(str)-2)): 
     i=ord(str[x]) 
     j=ord(str[x+1]) 
     k=ord(str[x+2]) 
     if((i+1)==j and (j+1)==k): 
      print("String has 3 consecutive letters") 
      flag=1 
      break 
     else: 
      x=x+1 
if(flag==0): 
    print("String has no 3 consecutive letters") 

当我在python中运行它时,它需要我输入连续的语句('then the word')。Python代码连续字母

我只是想能够键入单词,它告诉我它是否有连续3个字母。请帮忙!

+1

Python是压痕和结构极其依赖。请适当地格式化您的代码。 –

+1

请正确格式化您的代码,因为换行符和空格在Python中很重要。按4行缩进行,它们将被格式化为代码。 – Amber

+0

请在原始问题中格式化您的代码。您的代码编写方式,在语法上不正确,无法理解或执行。 – DyZ

回答

0
my_str = input('word: ') # get user input 
consecutiveLetter(my_str) # and pass it to your function 

你可以简化consecutiveLetter功能类似:

def consecutiveLetter(mystr): 
    for x in range(len(mystr)-2) : 
     if ord(mystr[x])+1 == ord(mystr[x+1]) and ord(mystr[x+1])+1 == ord(mystr[x+2]) : 
      print("String has 3 consecutive letters") 
      return 
    print("String has no 3 consecutive letters") 
+0

非常感谢tmadam!我看到我犯了什么错误。谢谢!谢谢!绝对是今晚吸取的教训!:) – eyrailey

+0

非常欢迎 –