简体   繁体   English

输入范围无效后如何重新提示循环用户输入

[英]How to re-prompt loop user input after invalid input range

User input range for num of rooms is [1-8].房间数量的用户输入范围是 [1-8]。 Invalid input for this should display 'invalid input' and keep reprompt until receive valid input for executing the rest of the program.此处的无效输入应显示'无效输入'并一直重新提示,直到收到有效输入以执行程序 rest。 I've made it so invalid input for services returns and 'invalid service', the same should be done for input of num of rooms.我已经将服务返回和“无效服务”的输入设置为无效,输入房间数也应如此。

def main():
    small = 60
    medium = 120
    large = 145
    servOne = 40
    servTwo = 80


    numRooms = int(input("Number of rooms in the house?: "))

    while True:
        restart = int(input("Sorry Invalid number of rooms,try again? "))
        if int(numRooms) < 1 or int(numRooms) > 8 in restart:
            continue
            (???)

       




    servType = input("Type of cleaning service requested? (carpet cleaning or window cleaning): ")


    if int(numRooms) <= 2:
        print("Calculating fees for small house...")

        if servType == "carpet cleaning":
            print("Total cost of service:$",(small+servOne))

        elif servType == "window cleaning":
            print("Total cost of service:$",(small+servTwo))

        else:
            print("Sorry! Invalid service input")



    if int(numRooms) >= 3 and int(numRooms) <= 5:
        print("Calculating fees for medium house...")

        if servType == "carpet cleaning":
            print("Total cost of service:$",(medium+servOne))

        elif servType == "window cleaning":
            print("Total cost of service:$",(medium+servTwo))

        else:
            print("Sorry! Invalid service input")



    if int(numRooms) >= 6:
        print("Calculating fees for large house...")

        if servType == "carpet cleaning":
            print("Total cost of service:$",(large+servOne))

        elif servType == "window cleaning":
            print("Total cost of service:$",(large+servTwo))

        else:
            print("Sorry! Invalid service input")

main()主要的()

just wrap input in a while loop.只需将输入包装在一个 while 循环中。 if valid input is recieved, you end the loop如果收到有效输入,则结束循环

valid_input=False 
while not valid_input:
    i=input("Number of rooms in the house?:")
    inp=int(i)
    if inp in range(1,9):  #(9 is not included)
        valid_input=True 
. 
.
remaining code 
.
.

another way to write above..easier to understand上面的另一种写法..更容易理解

while  1==1:  # repeat loop(all statements below) forever
    i=input("Number of rooms in the house?:")
    inp=int(i)
    if inp in range(1,9):  #(9 is not included)
        break # break out of the forever loop
#end of loop

One option (If I assume what you are trying to do) is to have a recursive function instead of a while loop, and call the function again on invalid input:一种选择(如果我假设你正在尝试做什么)是使用递归 function 而不是 while 循环,并在无效输入时再次调用 function :


def Main():
  validnums = [1,2,3]
  i = Input("Please Enter input")
  if int(i) not in validnums:
    Main()
  else:
    ##Do stuff
  Main()

By having Main() call Main(), it functions identical to a while loop but you can restart it anytime.通过让 Main() 调用 Main(),它的功能与 while 循环相同,但您可以随时重新启动它。 I hope this helps.我希望这有帮助。

If you want to avoid recursion:如果你想避免递归:

def Main():
  validnums = [1,2,3]
  While True:
    i = input()
    if int(i) not in validnums:
     break
  Main()

This uses a while loop and the function only calls itself when it needs to.这使用了一个 while 循环,而 function 仅在需要时调用自身。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM