简体   繁体   中英

How do i ask user for input continuously until they say "Stop"

I am creating a program where I have to ask the user the same question, until they type "Stop"

My issue is,

The user input has to be a float, but still be able to recognize when the user types "stop" so it will stop asking the question. Im having difficulty doing this.

while True:
    ans = input("Enter a number: ")
    #Currently ans variable is a string.
    #ans.lower() gives you a lowercase version of the string.
    if ans.lower()=="stop":
        break        #This command breaks out of the while loop.
    ans = float(ans)

    #You can do what you need to do from here after you get your float.

Something like that I would say

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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