简体   繁体   中英

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.

I think the program doesn't work as you want because you made two while loops: the first while's condition is while counter<=3 , ok but then you made another while which condition is ask !="" , so the program will run inside this second while loop until the condition is no more true and the first while does not "see" the counter's changes.

By the way, I think you can simply use only one while loop(the first one), and write an if condition that verify the value(>300).

When you try to cast a string element into a float type it will throw an error if the value could not be casted to that type, you could use a try-except block.

while counter < 3:
      try:
           ask = float(input("xxxxx")
           if ask >= 50:
               counter += 1
           else:
               print("xxxxx")
      except ValueError:
           break
print(list1)

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)

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