-3

The programm is check if input value is valid in list. below is my code. could you please help?thanks

while True:  
    try:
        count = 0
        Day_list = []
        days =  int(input("Enter number : "))
    except ValueError:
        print("Please input the integer")
    else:
        for i in range(days): 
            Day_list.append(float(input("Enter the day "+str(i+1)+ ": " )))
            if( 0<= Day_list[i] <=10):
   
                print('good' )
                                     
           else:
                print('Amount cant be negative, try again')
                break

i would like check the value one by one

eg. Enter the day 1 : -1

then output : Amount cant be negative, try again

return Enter the day 1 : 1

Enter the day 2 :

but i dont have idea where is mistake,thanks

iamnew
  • 27
  • 5
  • Maybe your indentation is wrong, should the `if( 0<= Day_list[i] <=10):` be nested under the for loop? – rassar Oct 26 '22 at 02:04
  • `Day_list[i] >= 0 and Day_list[i] <=10` – sifat Oct 26 '22 at 02:05
  • Hi, thanks all, I want to check every one, if the first day input value wrong, then output the message, but the program checks the all the value first , then show the wrong message. – iamnew Oct 26 '22 at 02:13
  • applying the suggestion by rassar (if nested in the loop), it worked for me: it makes the check number by number. Maybe you can show which are the values you are inputting and the actual vs. expected output. – Ignatius Reilly Oct 26 '22 at 02:26
  • Hi @IgnatiusReilly, thanks for your reply and support. how to input until a valid response. i modify my code and i try to input the wrong value, and then the program will show next step, how to return to back until input the valid value.thanks – iamnew Oct 26 '22 at 03:28
  • 1
    @iamnew maybe this [post](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) can help. – Ignatius Reilly Oct 26 '22 at 04:32

1 Answers1

0

here you are:

while True:  
    try:
        count = 0
        Day_list = []
        days =  int(input("Enter number : "))
    except ValueError:
        print("Please input the integer")
    else:        
        if days >= 0:
            for i in range(days):             
                valid = False
                while not valid:
                    try:    
                        Day_list.append(float(input("Enter the day "+str(i+1)+ ": " )))
                        if( Day_list[i] >= 0 and Day_list[i] <=10):
                            print('good' )
                            valid = True
                        
                    except ValueError:
                        print ('input is not valid')
                break                   
                    
        else:
            print('Amount cant be negative, try again')
  • Hi Henro Sutrisno Tanjung, thanks for your reply and support, how to check the value when i input the wrong data , the program will show the error message and then i need to input again, if it is vaild value , the program will be show next step? thanks – iamnew Oct 26 '22 at 03:15