Skip to content

Removed writing of if statements repetitivly#63

Open
diskeu wants to merge 1 commit intoGrow-with-Open-Source:mainfrom
diskeu:main
Open

Removed writing of if statements repetitivly#63
diskeu wants to merge 1 commit intoGrow-with-Open-Source:mainfrom
diskeu:main

Conversation

@diskeu
Copy link

@diskeu diskeu commented Jan 9, 2026

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.

@github-actions
Copy link

github-actions bot commented Jan 9, 2026

👋 @diskeu
Thank you for raising your pull request.
Please make sure you have followed our contributing guidelines. We will review it as soon as possible.

Copy link
Contributor

@iamwatchdogs iamwatchdogs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Contributor

@iamwatchdogs iamwatchdogs Feb 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This piece of code has the following concerns:

  • Using while True is not a good practice.
  • While trying to handle ValueError exception, 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 True with for loop 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.")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants