为了澄清,我已经粘贴了我的整个程序,因为只有当我的程序中包含所有代码时才能识别问题。我的问题是,我已经在我的程序中输入了else else和elif语句。当用户输入" no"程序结束,运行正常,如果用户输入"是"程序像它应该做的那样前进。问题是当用户输入无效的东西时,除了"是"或"不"它应该让他们重新输入他们的选项,但它继续进行,好像用户输入了"是"。
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
答案 0 :(得分:2)
您可以使用continue关键字。
继续声明,也是从C借来的,继续下一个 循环的迭代:
while True:
print (" Position Finder")
choice = (input("Do you want to run the Position Finder program? Please enter yes or no!"))
if choice.lower() == "no":
print("The program will not run")
quit()
elif choice.lower() == "yes":
print("The program will now run")
else:
print("Invalid entry, please type again")
continue # move to next iteration
rest_of_your_code()
答案 1 :(得分:1)
您只在else子句中编写了一个print语句:print("Invalid entry, please type again")
,但这并不会使程序重复前面的说明。一个可能的解决方案:
choice = ""
while choice.lower() != "yes":
choice = (input("Do you want to run the Position Finder program? Please enter yes or no!"))
if choice.lower() == "no":
print("The program will not run")
quit()
elif choice.lower() == "yes":
print("The program will now run")
else:
print("Invalid entry, please type again")
答案 2 :(得分:0)
你需要为此更多地放置一个while
循环,在语法错误的答案中返回用户并在正确的答案中打破循环。问题部分应如下所示:
while True: # Main loop
while True: # Question loop
choice = (input("Do you want to run the Position Finder program? Please enter yes or no!"))
if choice.lower() == "no":
print("The program will not run")
run = False
break
elif choice.lower() == "yes":
print("The program will now run")
run = True
break
else:
print("Invalid entry, please type again")
# In case of invalid entry Question loop will run again
# Outside Question loop
# Let's check if we should run the program
if not run:
break
# This will be executed if *run* is False, it will break the Main loop
# Your program