因此,我的任务是验证用户输入的每个 ISBN 10 位数字。我需要确保 1)用户输入不是空白,2)用户输入只是一个整数(我已经完成了),以及 3)他们只输入一位数字。
抱歉,我已经看到了一些类似的问题,但我想保留 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!")