2015-09-26 56 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'語句。 –