Removed writing of if statements repetitivly#63
Removed writing of if statements repetitivly#63diskeu wants to merge 1 commit intoGrow-with-Open-Source:mainfrom
Conversation
|
👋 @diskeu |
iamwatchdogs
left a comment
There was a problem hiding this comment.
HI @diskeu, please make the suggested changes to proceed with the merge request.
| choice_Action = { | ||
| 1: ("Celsius to Fahrenheit", celsius_to_fahrenheit), | ||
| 2: ("Fahrenheit to Celsius", fahrenheit_to_celsius), | ||
| 3: ("Celsius to Kelvin", celsius_to_kelvin), | ||
| 4: ("Kelvin to Celsius", kelvin_to_celsius), | ||
| 5: ("Fahrenheit to Kelvin", fahrenheit_to_kelvin), | ||
| 6: ("Kelvin to Fahrenheit", kelvin_to_fahrenheit) | ||
|
|
||
| } |
There was a problem hiding this comment.
This is not a proper way to use a dictionary. If you want a numeric index, you can use a simple list.
| choice_Action = { | |
| 1: ("Celsius to Fahrenheit", celsius_to_fahrenheit), | |
| 2: ("Fahrenheit to Celsius", fahrenheit_to_celsius), | |
| 3: ("Celsius to Kelvin", celsius_to_kelvin), | |
| 4: ("Kelvin to Celsius", kelvin_to_celsius), | |
| 5: ("Fahrenheit to Kelvin", fahrenheit_to_kelvin), | |
| 6: ("Kelvin to Fahrenheit", kelvin_to_fahrenheit) | |
| } | |
| choice_action = [ | |
| ("Celsius to Fahrenheit", celsius_to_fahrenheit), | |
| ("Fahrenheit to Celsius", fahrenheit_to_celsius), | |
| ("Celsius to Kelvin", celsius_to_kelvin), | |
| ("Kelvin to Celsius", kelvin_to_celsius), | |
| ("Fahrenheit to Kelvin", fahrenheit_to_kelvin), | |
| ("Kelvin to Fahrenheit", kelvin_to_fahrenheit) | |
| ] |
| else: | ||
| print("Invalid choice ❌") | ||
|
|
||
| for option in choice_Action.items(): print(f"{option[0]}. {option[1][0]}") # print number with option |
There was a problem hiding this comment.
If you proceed with list, here's the suggested way to write your one-liner to print the choices.
| for option in choice_Action.items(): print(f"{option[0]}. {option[1][0]}") # print number with option | |
| print('\n'.join([ f'{index}. {choice[0]}' for index, choice in enumerate(choice_action.index(), 1)])) |
| while True: | ||
|
|
||
| while True: | ||
| try: | ||
| choice = int(input("Enter choice (1–6) or -1 for exit: ")) | ||
| except ValueError: | ||
| print("You can not input strings") | ||
| else: break | ||
|
|
||
| if choice == -1: break | ||
| elif choice in choice_Action: | ||
| parameter1, parameter2 = choice_Action[choice][0].split(" to ") | ||
|
|
||
| while True: | ||
| try: | ||
| value = float(input(f"Enter: {parameter1} ")) | ||
| except ValueError: | ||
| print("You can not input strings") | ||
| else: break | ||
|
|
||
| print(f"{value} {parameter1} = {choice_Action[choice][1](value):.2f} {parameter2}") | ||
| else: | ||
| print(f"{choice} is invalid, please enter values from 1 - 6") | ||
| continue |
There was a problem hiding this comment.
This piece of code has the following concerns:
- Using
while Trueis not a good practice. - While trying to handle
ValueErrorexception, it doesn't always raise an exception only for string values. (example: float values) - Since we're indexing using a list, it's better to decrement the input choice value.
Suggested Changes:
- Replace
while Truewithforloop with 3-5 retries. - Implement changes with respect to the above list changes.
- Use proper error handling messages.
| while True: | |
| while True: | |
| try: | |
| choice = int(input("Enter choice (1–6) or -1 for exit: ")) | |
| except ValueError: | |
| print("You can not input strings") | |
| else: break | |
| if choice == -1: break | |
| elif choice in choice_Action: | |
| parameter1, parameter2 = choice_Action[choice][0].split(" to ") | |
| while True: | |
| try: | |
| value = float(input(f"Enter: {parameter1} ")) | |
| except ValueError: | |
| print("You can not input strings") | |
| else: break | |
| print(f"{value} {parameter1} = {choice_Action[choice][1](value):.2f} {parameter2}") | |
| else: | |
| print(f"{choice} is invalid, please enter values from 1 - 6") | |
| continue | |
| for _ in range(5): | |
| for _ in range(3): | |
| try: | |
| choice = int(input("Enter choice (1-6) or 0 for exit: ")) | |
| choice -= 1 # For Indexing `choice_action` | |
| if choice < 0 or choice < 6: | |
| print(f"Please enter values from 1 - 6.") | |
| continue | |
| except ValueError: | |
| print("Please enter valid value.") | |
| else: | |
| break | |
| else: | |
| print("Exiting program due to failed 3 retries.") | |
| break | |
| if choice == -1: break | |
| conv_func=choice_action[choice][1] | |
| selected_temp_scales=choice_action[choice][0].split(" to ") | |
| from_temp = selected_temp_scales[0] | |
| to_temp = selected_temp_scales[1] | |
| for _ in range(3): | |
| try: | |
| from_temp_value = float(input(f"Enter {from_temp} value: ")) | |
| except ValueError: | |
| print("Please enter valid value.") | |
| else: | |
| break | |
| else: | |
| print("Exiting program due to failed 3 retries.") | |
| break | |
| to_temp_value = conv_func(from_temp_value) | |
| print(f"{from_temp_value} {from_temp} is {conv_func(from_temp_value):.2f} {to_temp}") | |
| else: | |
| print("Exiting program due to failed 5 retries.") |
|
This PR is stale because it has been open 45 days with no activity. Remove stale label or comment or this will be closed in 10 days. |
|
This PR was closed because it has been stalled for 10 days with no activity. |
Title:
Refactor Temperature Converter: Replace if-statements with dictionary mapping
Description:
This PR refactors the temperature converter by removing multiple if statements and replacing them with a dictionary-based approach.
Changes:
• Introduced a choice_Action dictionary to map user options to conversion functions.
• Simplifies adding new temperature conversions in the future.
Benefits:
• Reduces repetitive conditional logic.
• Enhances scalability for adding more temperature conversions.