0
所以,我的任務是驗證用戶輸入的每個他們輸入的ISBN 10數字。我需要確保1)用戶輸入不是空白,2)用戶輸入只是一個整數(我已經完成),3)他們只輸入一個數字。ISBN檢查器 - 驗證用戶輸入
對不起,我已經看到了一些類似的問題,但我想保留try-except語句(如果可能),所以類似的問題不是太有用。
我該如何去驗證空白輸入,並且只輸入一位數字?
下面是代碼:
print("You will be asked to enter an ISBN-10 Number. Please enter it digit by digit.")
ISBN10NumberList = []
ISBN10NumberAdder = 0
for count in range (10):
validInput1 = True
if (count <= 8):
while validInput1 != False:
try:
ISBN10NumberList.append(int(input("Please enter the ISBN digit: ")))
validInput1 = False
except ValueError:
print("That is not a valid input! Please enter a integer only.")
elif (count == 9):
CheckDigit10 = input("Please enter the ISBN digit: ")
print("")
if CheckDigit10 == "X" or CheckDigit10 == "x":
CheckDigit10 = 10
for count in range (0, 9):
ISBN10NumberAdder += int(ISBN10NumberList[count]) * (10 - count)
CheckDigit10 = int(CheckDigit10)
CheckingCheckDigit = 11-(ISBN10NumberAdder % 11)
if (CheckDigit10 == CheckingCheckDigit):
print("This is a valid ISBN!")
else:
print("This is not a valid ISBN!")
按位數輸入?這是什麼模式,由Sadism編程? – 2015-02-05 21:43:42
哈哈哈,好點。 – electricbl00 2015-02-05 21:45:45
Ignacio有一個很好的觀點。爲什麼不把整個輸入(作爲一個字符串),通過char遍歷它來做你的理智檢查,然後在最後執行有效性檢查? – MattDMo 2015-02-05 21:51:49