Home > Enterprise >  Ask User to re enter string if it doesnt match to items in list
Ask User to re enter string if it doesnt match to items in list

Time:10-31

im having trouble asking the user to re-enter input if it doesn't match string values from specific list. what have i missed?


El_List = 'H', 'He', 'Li', 'Be', 'B', 'C', 'N', 'O', 'F', 'Ne'


user_instructions = ( 'Select Elements value from following list to create a molecule')

print (user_instructions   )   
                      
                               
while true :
    try :
        elem = str(input('add molecular formula: '))
        if elem = 'H' or = 'He' or = 'Li' or = 'Be' or = 'B' or = 'C' or = 'N' or = 'O' or = 'F' or = 'Ne':
            print (elem)
           break,
        else : 
            print ('Not Found in Element list)
            
        except: ValueError : 
            print('add molecular formula: ')
            continue

CodePudding user response:

Welcome to SO. First of all, you have a lot of indentation errors in your code. I just tried on my side after cleaning up the code and is working fine.

Try this:

El_List = 'H', 'He', 'Li', 'Be', 'B', 'C', 'N', 'O', 'F', 'Ne'


user_instructions = ( 'Select Elements value from following list to create a molecule')

print (user_instructions)   
                  
                           
while True:
    try :
        elem = str(input('add molecular formula: '))
        if elem == 'H' or elem == 'He' or elem == 'Li' or elem == 'Be' or elem == 'B' or elem == 'C' or elem == 'N' or elem == 'O' or elem == 'F' or elem == 'Ne':
        print(elem)
        break

        else : 
            print ('Not Found in Element list')
        
    except ValueError : 
        print('add molecular formula: ')
        continue

Your except is inside your try statement. Make sure all the indentations are correct. Hope this helps!

  • Related