Prompt the user to input something else if the first input is invalid

后端 未结 2 1043
孤城傲影
孤城傲影 2020-11-30 15:42

I\'m very new to Python, so forgive my newbish question. I have the following code:

[a while loop starts]

print \'Input the first data as 10 characters from         


        
2条回答
  •  一个人的身影
    2020-11-30 16:39

    To go on with the next loop iteration, you can use the continue statement.

    I'd usually factor out the input to a dedicated function:

    def get_input(prompt):
        while True:
            s = raw_input(prompt)
            if len(s) == 10 and set(s).issubset("abcdef"):
                return s
            print("The only valid inputs are 10-character "
                  "strings containing letters a-f.")
    

提交回复
热议问题