Python Forum
How to keep looping until the user input is valid ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to keep looping until the user input is valid ?
#13
def get_valid_input(value):
    while True:
        try:
            return float( input( value ) )
        except ValueError:
            print( 'LOL, your input is not valid, please try again!!' )
...
...
CABLES = [10, 16, 25, 35, 50, 70, 95, 120, 150, 185, 240, 300, 400, 500, 630]
...
...
def get_valid_cable_size():
    print('Please take a note that acceptable cable size (sq.mm) are as follow: ')
    print(CABLES)
    while True:
        cableSize = get_valid_input( 'Please enter a cable size:' )
        cableSize = int(cableSize)
        if cableSize in CABLES:
            return cableSize
        print('Please enter a valid cable size')
...
...
def installation_methods():
    print( 'Please choose the installation method\n\
               (1) Mobile installation in open air, ambient temperature 30 deg C\n\
               (2) Fixed and protected installation in open air, ambient temperature 30 deg C' )
    while True:
        installation_method = get_valid_input( '1 or 2?' )
        if installation_method in (1, 2):
            return installation_method
...
...
def get_required_set_of_cables(Ampere, cableSize):


    while installation_method == 1:
        if cableSize == 10:
            return Ampere / 53
        elif cableSize == 16:
            return Ampere / 71
        elif cableSize == 25:
            return Ampere / 94
        elif cableSize == 35:
            return Ampere / 117
        elif cableSize == 50:
            return Ampere / 148
        elif cableSize == 70:
            return Ampere / 185
        elif cableSize == 95:
            return Ampere / 222
        elif cableSize == 120:
            return Ampere / 260
        elif cableSize == 150:
            return Ampere / 300
        elif cableSize == 185:
            return Ampere / 341
        elif cableSize == 240:
            return Ampere / 407
        elif cableSize == 300:
            return Ampere / 468
        elif cableSize == 400:
            return Ampere / 553
        elif cableSize == 500:
            return Ampere / 620
        elif cableSize == 630:
            return Ampere / 742

    while installation_method == 2:
        if cableSize == 10:
            return Ampere / 77
        elif cableSize == 16:
            return Ampere / 102
        elif cableSize == 25:
            return Ampere / 136
        elif cableSize == 35:
            return Ampere / 168
        elif cableSize == 50:
            return Ampere / 203
        elif cableSize == 70:
            return Ampere / 254
        elif cableSize == 95:
            return Ampere / 299
        elif cableSize == 120:
            return Ampere / 363
        elif cableSize == 150:
            return Ampere / 416
        elif cableSize == 185:
            return Ampere / 475
        elif cableSize == 240:
            return Ampere / 559
        elif cableSize == 300:
            return Ampere / 637
        elif cableSize == 400:
            return Ampere / 722
        elif cableSize == 500:
            return Ampere / 833
        elif cableSize == 630:
            return Ampere / 833
...
...
if __name__ == '__main__':
    while True:
        print('******This program\'s algorithm behind is only for 1C H07RN-F insulated cable. Developed by Kyaw Myo Oo*****\n'
              'Also please take a note that there is no consideration for voltage drop and other factor such as Ca, Cg & Ci. ')
        Ampere = get_valid_input( 'Please enter an Ampere:' )
        cableSize = get_valid_cable_size()
        installation_method = installation_methods()
        required_set_of_cables = get_required_set_of_cables(Ampere, cableSize)
        print(f'The cables sets required for {cableSize}sq.mm cable are{required_set_of_cables: .2f} sets per phase for {Ampere}A' )
        print('The program ends here! Good Luck!!\n-----------------------------------')
Finally, my small program successfully works. Thanks Yoriz for the guide and teach me....Thanks you so much.....Finally, I fall in love with Python. I am from non-IT industry, working as electrical engineer...
Reply


Messages In This Thread
RE: How to keep looping until the user input is valid ? - by KyawMyo - Jun-10-2019, 02:51 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  is this a valid variable name? Skaperen 5 239 8 hours ago
Last Post: snippsat
  Script getting reindexing only valid error cubangt 1 1,233 Dec-07-2023, 04:06 PM
Last Post: cubangt
  WHILE LOOP NOT RETURNING USER INPUT AFTER ZerroDivisionError! HELP! ayodele_martins1 7 1,248 Oct-01-2023, 07:36 PM
Last Post: ayodele_martins1
  restrict user input to numerical values MCL169 2 1,032 Apr-08-2023, 05:40 PM
Last Post: MCL169
Question Use function, retry until valid Ashcora 8 1,597 Jan-06-2023, 10:14 AM
Last Post: Ashcora
  user input values into list of lists tauros73 3 1,176 Dec-29-2022, 05:54 PM
Last Post: deanhystad
Information How to take url in telegram bot user input and put it as an argument in a function? askfriends 0 1,216 Dec-25-2022, 03:00 PM
Last Post: askfriends
Question Take user input and split files using 7z in python askfriends 2 1,205 Dec-11-2022, 07:39 PM
Last Post: snippsat
Sad how to validate user input from database johnconar 3 2,097 Sep-11-2022, 12:36 PM
Last Post: ndc85430
  How to split the input taken from user into a single character? mHosseinDS86 3 1,272 Aug-17-2022, 12:43 PM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020