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 ?
#11
You have the same variable storing the text that is displayed when asking for input as the one that is storing the actual cable size.
as 'Please enter the cable size...:' will always be used inside of get_valid_cable_size you could remove the need to pass this in as a parameter.
It would also be good to let the user know what choices of cable size are available
CABLES = (25, 35, 50, 70, 95, 120, 150, 195, 250, 300, 500) ... ... def get_valid_cable_size(): print('Valid cable sizes are as follows:') print(', '.join(map(str, CABLES))) while True: cableSize = get_valid_input('Please enter the cable size...:') if cableSize in CABLES: return cableSize print('Please enter a valid cable size') ... ... cableSize = get_valid_cable_size() print(cableSize)
Output:
Valid cable sizes are as follows: 25, 35, 50, 70, 95, 120, 150, 195, 250, 300, 500 Please enter the cable size...:21 Please enter a valid cable size Please enter the cable size...:25 25.0
Reply
#12
Thanks! Finally my program works. really happy learning. looking forward to another program.....will ask question again if get unsolved error myself...
Reply
#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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Identifying Valid and Invalid Phone Contact KiranKandavalli 3 1,351 Apr-03-2025, 07:12 PM
Last Post: deanhystad
  How to revert back to a previous line from user input Sharkenn64u 2 2,992 Dec-28-2024, 08:02 AM
Last Post: Pedroski55
  User input with while loops chizzy101010 2 12,214 Aug-25-2024, 06:00 PM
Last Post: chizzy101010
  is this a valid variable name? Skaperen 6 2,740 Jun-05-2024, 10:13 PM
Last Post: Skaperen
  Script getting reindexing only valid error cubangt 1 5,217 Dec-07-2023, 04:06 PM
Last Post: cubangt
  WHILE LOOP NOT RETURNING USER INPUT AFTER ZerroDivisionError! HELP! ayodele_martins1 7 3,599 Oct-01-2023, 07:36 PM
Last Post: ayodele_martins1
  restrict user input to numerical values MCL169 2 2,513 Apr-08-2023, 05:40 PM
Last Post: MCL169
Question Use function, retry until valid Ashcora 8 4,651 Jan-06-2023, 10:14 AM
Last Post: Ashcora
  user input values into list of lists tauros73 3 2,836 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 3,073 Dec-25-2022, 03:00 PM
Last Post: askfriends

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.