好,所以我需要做的是讓我的代碼只允許用戶一次輸入一個字母,然後輸入一個符號。下面的例子顯示了我想要的更好的視角。如何讓用戶一次只輸入一個字符
此刻我的代碼允許用戶在一次我不想要的時間輸入多個字符。
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
爲什麼不讓用戶輸入一個字母和一個配對,用空格隔開?這對你和用戶來說也會更容易:'H1 B4 C3'等等。而且,評論應該爲你的代碼增加意義,只需添加你所做的註釋就可以分散注意力。 – 2014-09-28 19:00:41
我該怎麼做,你能爲我編輯代碼嗎? – Paul 2014-09-28 19:01:08
'a,b,c = raw_input(「輸入三個用空格分隔的符號」)。split()' – 2014-09-28 19:02:37