0

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.

1 Answers1

1
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

Paul Kim
  • 126
  • 6
  • It's a bit old but it might be worth checking the type of the input before blindly attempting type conversion to float. Might want to look into try... except... statements – Paul Kim Sep 19 '22 at 22:45