【发布时间】:2016-04-14 21:42:29
【问题描述】:
为了澄清,我已经粘贴了我的整个程序,因为只有在我的程序中的所有代码出现问题时才能识别问题。我的问题是,我在我的程序中输入了 if else 和 elif 语句。当用户输入“否”时,程序结束,并且运行良好,如果用户输入“是”,则程序按预期进行。问题是当用户输入一些无效的内容时,除了“是”或“否”之外,它应该让他们重新输入他们的选项,但它会继续进行,就好像用户输入了“是”一样。
while True:
print (" Position Finder")
choice = (input("Do you want to run the Position Finder program? Please enter yes or no!"))
# Lets the user input an answer to question I am asking it
if choice.lower() == "no":
# If the user input is "no"
print("The program will not run")
# This statement will be printed out and the program will not run
quit()
elif choice.lower() == "yes":
# If the user input is "yes"
print("The program will now run")
# This statement will be printed and the program will run
else:
# If an invalid statement is inputted
print("Invalid entry, please type again")
# This statement will be printed and the user will be given the option to input again
sentList = "ask not what your country can do for you ask what you can do for your country"
# Creates a list
sentList2 = sentList.split()
# This will split the list and give each word a position
print (sentList)
# This will print out the sentence in the program
word = (input("Enter Word: "))
# Lets the user input a word from the sentence
wordFound = False
# This boolean statement, "wordFound" will be set to true when the word has been found in the list
for (num, x) in enumerate(list(sentList2)):
# For every word in the users sentence
while word.lower() == x:
# While the users word is equal to the position in the sentence
print (ordinalSuffix())
# Print the ordinal suffix function
wordFound = True
# This boolean statement has been set to true as the word has been found in the list
break
# This will break the while loop from the boolean variable called "wordFound"
if wordFound == False:
# If the word has not been found in the list and the "wordFound" boolean variable has not been set to true
print ("Please enter a word from the sentence")
# This statement will print,meaning the user has to enter a word from the list
【问题讨论】:
标签: python-3.x