Home > Back-end >  How do i ask user for input continuously until they say "Stop"
How do i ask user for input continuously until they say "Stop"

Time:03-15

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.

CodePudding user response:

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

CodePudding user response:

Check if the value is "stop", then parse the value into a float using float().

if (input == "stop") 
  #exit loop
floatInput = float(input)

See also this question.

  • Related