exception handling - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: exception handling (/thread-18113.html) |
exception handling - KyawMyo - May-06-2019 try: power = float(input ("Please entre Power in kW: ")) voltage = float (input ("Please entre Voltage: ")) phase = int(input("1 or 3 phase? ")) except: print("Oops! That was no valid number. Try again...") if phase==1: current = (power/voltage)*1000 print("The current is :" ,round(current, 2),"Amp") if phase==3: current = (power/(1.732*voltage))*1000 print("The current is :" ,round(current, 2),"Amp") input()Hi All, What to retry after exception. How to write the code. Let says the user input wrongly in voltage, just want to go back to voltage after exception. not from start. Also for the phase, I just want to accept 1 or 3, how to code. RE: exception handling - Gribouillis - May-06-2019 Try this def input_float(message): while True: try: return float(input(message)) except ValueError: print('Oops, that was not a valid number. Try again...') voltage = input_float('Please enter voltage:') RE: exception handling - KyawMyo - May-07-2019 while True: def input_power(value): while True: try: return float(input(value)) except ValueError: print('Oops, that was not a valid number. Try again...') power = input_power('Please enter power in kW:') def input_voltage(value): while True: try: return float(input(value)) except ValueError: print('Oops, that was not a valid number. Try again...') voltage = input_voltage('Please enter voltage:') def input_phase(value): while True: try: return float(input(value)) except ValueError: print('Oops, that was not a valid number. Try again...') phase = input_phase('1 or 3 phase? ') if phase==1: current = (power/voltage)*1000 print("The current is :" ,round(current, 2),"Amp") if phase==3: current = (power/(1.73205*voltage))*1000 print("The current is :" ,round(current, 2),"Amp") input()Finally, got it. learned new things. Thanks! RE: exception handling - buran - May-07-2019 (May-07-2019, 01:40 AM)KyawMyo Wrote: Finally, got it. learned new things. Thanks! One of main principles is don't repeat yourself (DRY) and you are doing exactly that. Think what your function should do and how you want it to be customizable. That's what Gribouillis's example is - general implementation that can be customized by passing an argument (message/prompt to be displayed). You can use it virtually everywhere to get float number input from user. Then couple of more issues:
def get_float(message): while True: try: return float(input(message)) except ValueError: print('Oops, that was not a valid number. Try again...') def calculate_current(phase): phase = int(phase) if phase not in (1, 3): raise ValueError(f'Incorrect phase. Expecting 1 or 3, got {phase}') k = 1.73205 if phase == 3 else 1 # use appropiate symbol, if there is accepted one - I just don't know return (power/(k * voltage))*1000 if __name__ == '__main__': while True: power = get_float('Please enter power in kW:') voltage = get_float('Please enter voltage:') phase = get_float('1 or 3 phase? ') try: current = calculate_current(phase) except ValueError as ve: print(ve) else: print(f'The current is {current:.2f} Amp') if input('Do you want to calculate again (yes/no)? ').lower() in ('no', 'n'): breakThere are things that eventually can be made better or differently, but you get the idea |