我有一个while语句,它运行良好,我有一段代码要求用户输入他们有多少名称,然后会询问他们一个名称,每次输入一个名字。错误点击一段代码
我需要输入的名称部分是错误的窃听,但我不知道该怎么做,因为我有一个while语句,我可能需要把另一个while语句,虽然我有错误点击部分数字中的姓名数量。
而且有进一步的代码与字典和排序,但我需要有错误敲击的一个部分帮助开始在同时currentnum部分
print("Please enter each name when asked without any spaces.") #The program will post this
print("Please enter each of your names individually also.") #Program will again post this
names = [] #This is the value of names which will be changed depending on the input
currentnum = 0 #Currentnum value is 0
while True: #While loop as it will revert to the start if question answered incorrectly
try:
numofnames = int(input("How many names do you have? "))
except ValueError: #if the input is not an integer or a whole number it will
print("Sorry that was not a valid input please retry")
continue #it will loop back and ask the question again as it says that the unput was not valid
else:
break #If the input is correct then the loop will break and continue to the next section of the program
while currentnum < numofnames: #This means that while currentnum is smaller than input for numofnames it will continue to ask question. This is another loop
currentnum = currentnum + 1 # every time the question is asked it means that currentnum gets 1 added to it and will continue to ask untill it is the same as the input for numofnames
name = str(input("Enter your name: ")) #Name asked to be entered in string
name = name.upper() #This changes all letters to upper case no matter what so there is no error for upper and lower case or a bigger dictionary showing lower and upper case values.
names.append(name)
快速的问题。 “点击错误”是什么意思? – Kevin
像这样,如果有人把数字或什么东西,我不想让他们然后程序将关闭,并说你只能在这种情况下放在字母 –