2016-03-06 117 views
-2

我寫了一個程序來獲取用戶的單詞,並將這些單詞的複數作爲輸出。現在我不能做以下兩件事如何獲得確切的輸出?

1 - 如何限制隻字符串輸入,即如果用戶輸入整數,那麼程序必須拋出錯誤。

2 - 輸入 - 貓墊蝙蝠輸出 - >貓草蓆蝙蝠 輸入 - 貓,墊,蝙蝠輸出 - >貓,S墊,S蝙蝠,S(我想避免這項即,當用戶通過分離話逗號然後我應該得到蝙蝠不蝙蝠,s)

請引導我在這裏,請注意縮進。

謝謝

`(def plural(user_input): 
    # creating list 
    List_of_word_ends = ['o','ch', 's', 'sh', 'x', 'z'] 
    words = user_input.split() 
    ws = ""; 
    # setting loop to for words 
    for word in words: 
     if len(word)>0 : 
      if word.endswith("y"): 
       word = word[:-1] 
       word += "ies" 
      else: 
       isSomeEs = False; 
       for suffix in List_of_word_ends: 
        if word.endswith(suffix): 
         word += "es" 
         isSomeEs = True; 
         break 
       if not isSomeEs: 
        word += "s" 
      ws += word+" " 
    print ws 
# taking input from user 
singular = raw_input("Please enter the words whose plural you want:") 
# returns a list of words 
x = singular.split(" ") 
x = singular.split(",") 
#calculate the length of object 
y = len(x) 
print "The no. of words you entered is :", y 
#function call 
plural(singular))` 
+0

你的問題是目前「我的程序都有漏洞,缺乏特色;請完成它對於我來說,」這是1)不是一個簡單的問題,和2)實際上不是一個問題,因此不是針對SO的話題。 – TigerhawkT3

+0

對不起,我忘了 – siddpro

+0

有些部分似乎對我來說可挽救,但不是全部。做一些關於問題1的搜索,並對其進行良好的刺探,我敢打賭,我們可以幫助你(可能在閱讀幫助中心後發佈一個新問題)。問題2在本網站的範圍內無法通過任何方式處理 - 英語語言太複雜,無法適用於SO答案 –

回答

0

回答你的第一個問題

# taking input from user 
singular = raw_input("Please enter the words whose plural you want:") 

try: 
    val = int(singular) 
except ValueError: 
    print("Please enter a valid string. Integer not accepted!!!") 
+0

關於你的第二個問題: 請輸入你想要的複數詞:貓,墊,蝙蝠 輸入單詞之間沒有任何空格。你的函數中使用的split()函數根據空格拆分,所以不要給出任何空格。 請輸入您想要的複數詞:cat,mat,bat No。你輸入的單詞是:3 貓,墊子,蝙蝠 如果您對答案滿意,請將其標記爲已回答 –

-1

關於第一個要求 - 你可以不喜歡簡單的東西:

如果奇異==類型(5): 打印 '錯誤'

5中類型 - 可以被任何int替換。

關於第二個要求 - 不確定你到底在找什麼......如果你可以請你提供一個例子。

+0

'singular'總是一個字符串,如果它是一個表示整數的字符串,它仍然不是一個整數,它絕對不會是一個類型。這完全錯了。此外,答案不是問題或澄清的地方 - 而是使用評論。 – TigerhawkT3

0

爲了幫助你與你的第一個問題,輸入型的問題,添加try/catch只要你有用戶輸入。像這樣的東西應該工作:

singular = raw_input("Please enter the words whose plural you want. ") 
try: 
    int(singular) 
except ValueError: 
    print "Invalid input! Please try again."