Q:

Python how to stop adding into list after hitting certain criteria

I'm doing a project whereby a user has to input the value. The loop should end if the user key in the value of more than 300 for 3 times. If the user key in a value lesser than 300, a warning message should be prompted. And another criteria is that I need to allow the user to get out of the loop if the user do not meet the condition above. For now, I tried to do by using the list but my code doesn't seem to count the number of inputs.
list1 = list()
counter = 1 
while counter <= 3: 
    ask = float(input("Please enter each of the value: ")) 
    while ask != "":
        list1.append(str(ask))
        ask = float(input("Please enter each of the value: "))
        if ask >= 50:
            counter += 1 
        else:
            print("Value must be more than 300. If you do not meet the criteria, please press 'enter'. ")
print(list1)

The following code is my original code which doesn't take into account of the minimum input value.
counter = 1 
while counter <= 3:
    ask = float(input("Please enter each of the value: ")) 
    if ask >= 50:
        counter += 1 
    else:
        print("Value must be more than 300 ")

I would appreciate if anyone of you can help me out.

A:

The problem is your inner while loop can't exit since ' "" ' (your exit signal) cannot be converted to float. Instead it will raise a ValueError.
One possible solution to this would be to try converting input to float and except the ValueError. It could look something like this.
list1 = list()
counter = 1
while counter <= 3:
    try:
       ask = float(input("Please enter each of the value: "))
    except ValueError:
       break
    list1.append(str(ask))
    if ask >= 50:
        counter += 1 
    else:
        print("Value must be more than 300. If you do not meet the criteria, please press 'enter'. ")

print(list1)

