2014-09-28 79 views
0

好,所以我需要做的是讓我的代碼只允許用戶一次輸入一個字母,然後輸入一個符號。下面的例子顯示了我想要的更好的視角。如何讓用戶一次只輸入一個字符

此刻我的代碼允許用戶在一次我不想要的時間輸入多個字符。

What letter would you like to add? hello 

What symbol would you like to pair with hello 
The pairing has been added 
['A#', 'M*', 'N', 'HELLOhello'] 

我想要的是一條消息,像這樣顯示,配對不會被添加到列表中。

What letter would you like to add? hello 

What symbol would you like to pair with hello 
You have entered more than one character, the pairing was not added 
['A#', 'M*', 'N',]. 

到目前爲止,我對於這部分代碼如下...

這也將是偉大的,當用戶輸入的文字部一數,要打印的錯誤消息。

def add_pairing(clues): 
    addClue = False 
    letter=input("What letter would you like to add? ").upper() 
    symbol=input("\nWhat symbol would you like to pair with ") 
    userInput= letter + symbol 
    if userInput in clues: 
     print("The letter either doesn't exist or has already been entered ") 
    elif len(userInput) ==1: 
     print("You can only enter one character") 
    else: 
     newClue = letter + symbol 
     addClue = True 
    if addClue == True: 
     clues.append(newClue) 
     print("The pairing has been added") 
     print (clues) 
    return clues 
+0

爲什麼不讓用戶輸入一個字母和一個配對,用空格隔開?這對你和用戶來說也會更容易:'H1 B4 C3'等等。而且,評論應該爲你的代碼增加意義,只需添加你所做的註釋就可以分散注意力。 – 2014-09-28 19:00:41

+0

我該怎麼做,你能爲我編輯代碼嗎? – Paul 2014-09-28 19:01:08

+0

'a,b,c = raw_input(「輸入三個用空格分隔的符號」)。split()' – 2014-09-28 19:02:37

回答

0

,以確保用戶輸入的最簡單的方法是使用一個循環:

while True: 
    something = raw_input(prompt) 

    if condition: break 

東西成立這樣會繼續問prompt直到condition滿足。你可以讓你想測試condition東西,所以對你來說,這將是len(something) != 1

0

如果你讓用戶輸入一個字母和符號對你的方法可以簡化爲以下幾點:

def add_pairing(clues): 
    pairing = input("Please enter your letter and symbol pairs, separated by a space: ") 
    clues = pairing.upper().split() 
    print('Your pairings are: {}'.format(clues)) 
    return clues 
+0

爲什麼使用'.split('')'? – 2014-09-28 19:06:47

+0

您並不需要將'clues'作爲參數傳遞給它,因爲您正在爲該函數內分配一個全新的值。 – MattDMo 2014-09-28 19:07:24

+0

不幸的是,這似乎不適用於我的程序的其餘部分... – Paul 2014-09-28 19:09:21

0

我很喜歡在類似的情況下提高和捕捉異常。 「C-ish」背景(sloooow)的人可能會感到震驚,但在我看來,它完全是pythonic,非常易讀和靈活。

此外,你應該爲角色添加,查看外面套你期待:

import string 

def read_paring(): 
    letters = string.ascii_uppercase 
    symbols = '*@#$%^&*' # whatever you want to allow 

    letter = input("What letter would you like to add? ").upper() 
    if (len(letter) != 1) or (letter not in letters): 
     raise ValueError("Only a single letter is allowed") 

    msg = "What symbol would you like to pair with '{}'? ".format(letter) 
    symbol = input(msg).upper() 
    if (len(symbol) != 1) or (symbol not in symbols): 
     raise ValueError("Only one of '{}' is allowed".format(symbols)) 

    return (letter, symbol) 

def add_pairing(clues): 
    while True: 
     try: 
      letter, symbol = read_paring() 
      new_clue = letter + symbol 
      if new_clue in clues: 
       raise ValueError("This pairing already exists") 
      break # everything is ok 
     except ValueError as err: 
      print(err.message) 
      print("Try again:") 
      continue 

    # do whatever you want with letter and symbol 
    clues.append(new_clue) 
    print(new_clue) 
    return clues 
+0

我收到一個錯誤,讓我給你一個鏈接到我的代碼,你可能會弄清楚爲什麼... http://www.codeshare.io/mgBKs – Paul 2014-09-28 19:46:54

+0

我用raw_input()(python 2.x )。你清楚地使用python 3.x,所以檢查更新的版本(使用'input()') – 2014-09-28 19:48:39

+0

現在得到一長串錯誤... http://www.codeshare.io/mgBKs – Paul 2014-09-28 19:52:44

0

不清楚自己想回到什麼,但是這將檢查所有條目:

def add_pairing(clues): 
    addClue = False 
    while True: 
     inp = input("Enter a letter followed by a symbol, separated by a space? ").upper().split() 
     if len(inp) != 2: # make sure we only have two entries 
      print ("Incorrect amount of characters") 
      continue 
     if not inp[0].isalpha() or len(inp[0]) > 1: # must be a letter and have a length of 1 
      print ("Invalid letter input") 
      continue 
     if inp[1].isalpha() or inp[1].isdigit(): # must be anything except a digit of a letter 
      print ("Invalid character input") 
      continue 
     userInput = inp[0] + inp[1] # all good add letter to symbol 
     if userInput in clues: 
      print("The letter either doesn't exist or has already been entered ") 
     else: 
      newClue = userInput 
      addClue = True 
     if addClue: 
      clues.append(newClue) 
      print("The pairing has been added") 
      print (clues)    
      return clues 
+0

打印後稱語法無效「字符數量不正確」 – Paul 2014-09-28 19:55:28

+0

忘記您正在使用python 3,現在正在運行,測試它,應 – 2014-09-28 19:56:19

+0

當我運行的代碼,並選擇添加配對我得到這個... http://www.codeshare.io/ADoW0 – Paul 2014-09-28 19:59:16

相關問題