Removed writing of if statements repetitivly#63
Open
diskeu wants to merge 1 commit intoGrow-with-Open-Source:mainfrom
Open
Removed writing of if statements repetitivly#63diskeu wants to merge 1 commit intoGrow-with-Open-Source:mainfrom
diskeu wants to merge 1 commit intoGrow-with-Open-Source:mainfrom
Conversation
|
👋 @diskeu |
iamwatchdogs
requested changes
Feb 1, 2026
Contributor
iamwatchdogs
left a comment
There was a problem hiding this comment.
HI @diskeu, please make the suggested changes to proceed with the merge request.
Comment on lines
+27
to
+35
| 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) | ||
|
|
||
| } |
Contributor
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.
Suggested change
| 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 |
Contributor
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.
Suggested change
| 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)])) |
Comment on lines
+41
to
+64
| 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 |
Contributor
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.
Suggested change
| 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 file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.