2015-09-26 80 views
1

我想写一个剧本需要一个字和印刷前三个字符,最后3个字符,不管位于正中间点:参数扩展使用Python

abracabra

ABR .. .bra

我做了工作,

word = input("What's the word ?") 
first = str(word[0:3]) 
last = str(word[-3:]) 
middle = int(len(word)-6) 
midDOTS = "." * (middle) 
print((first)+(midDOTS)+(last)) 

,但我想这样做在同一行,就像我可以在bash做,例如它返回的网络接口的列表:

INTFACES=$(/sbin/ifconfig -a | sed 's/[ \t].*//;/^\(lo\|\)$/d') 

如何用python做到这一点?我试过了,但没有奏效:

word = input("What's the word ?") 
midDOTS = "." * (int(len(word)-6)) 
print(str(word[0:3])+(midDOTS)+ str(word[-3:])) 

什么是正确的语法?

编辑

感谢大家对我的帮助,不仅得到这个正确的,但把它理解为好。以下是我结束了去...

def print_dotted_word(): 
    word = str(input("What's the word ?")) 
    if len(word)<7: 
     raise ValueError("Needs to be at least 7 letters.") 
    print(word[:3] + '.'*(len(word)-6) + word[-3:]) 

while True: 
    try: 
     print_dotted_word() 
     break 
    except ValueError:("Needs to be at least 7 letters.") 
+0

你有一个语法错误,'midDOTS = “” *(int(len(word)''应该是'midDOTS =“。”*(int(len(word)-6))' – dstudeba

+0

是的,我刚修好了,谢谢,但脚本仍然不能运行。 –

+1

Oh waiting - You are right!It does now now!非常感谢。 –

回答

1

你可以做到以下几点:

word = input("What's the word ?") 
if len(word)<7: 
    raise ValueError("Please enter a word greater than 6 characters") 
print(word[:3] + '.'*(len(word)-6) + word[-3:]) 

在这里,我们将提出一个ValueError异常,如果进入了word少于7个字符。

我们可以通过一个函数print_dotted_word()封闭该代码在Python Shell检查。

的Python 2.7:

In [1]: def print_dotted_word(): 
      word = raw_input("What's the word ? \n") # use raw_input 
      if len(word)<7: # check for word length 
       raise ValueError("Please enter a word greater than 6 characters") # raise exception 
      print word[:3] + '.'*(len(word)-6) + word[-3:] # print desired response 

In [2]: print_dotted_word() 
What's the word ? 
helloworld 
hel....rld 

In [3]: print_dotted_word() 
What's the word ? 
hello 
--------------------------------------------------------------------------- 
ValueError        Traceback (most recent call last) 
----> 1 print_dotted_word() 
     2  word = raw_input("What's the word ? \n") 
     3  if len(word)<7: 
----> 4   raise ValueError("Please enter a word greater than 6 characters") 
     5  print word[:3] + '.'*(len(word)-6) + word[-3:] 

ValueError: Please enter a word greater than 6 characters 

的Python 3.4:

In [1]: def print_dotted_word(): 
      word = input("What's the word ? \n") # use input here 
      if len(word)<7: # check for word length 
       raise ValueError("Please enter a word greater than 6 characters") # raise exception 
      print(word[:3] + '.'*(len(word)-6) + word[-3:]) # print desired response 

In [2]: print_dotted_word() 
What's the word ? 
helloworld 
hel....rld 

In [3]: print_dotted_word() 
What's the word ? 
hello 
--------------------------------------------------------------------------- 
ValueError        Traceback (most recent call last) 
----> 1 print_dotted_word() 
     2  word = input("What's the word ? \n") 
     3  if len(word)<7: 
----> 4   raise ValueError("Please enter a word greater than 6 characters") 
     5  print(word[:3] + '.'*(len(word)-6) + word[-3:]) 
     6 
ValueError: Please enter a word greater than 6 characters 
+1

好了,我看到你这个打算,但程序仍然崩溃,我对python很陌生,但熟悉bash,我看到这是一个if语句,不应该有其他错误来捕捉错误吗?除了我用错误消息获得回溯外,一切都很好。我该如何处理这个问题? –

+1

没关系,我明白了: –

+0

如果输入字符串小于7个字符,程序会引发一个'ValueError'异常。如果没有捕获到异常,则会导致程序执行停止。如果你想让程序不崩溃,你需要像上面那样添加一个'try/except'。不,应该没有'else',因为只有'if'情况下才会引发异常。另外,我们使用'try/except'来在Python中捕获异常,而不是'else'语句。 –