Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 35 additions & 30 deletions Temperature_Converter/temperature_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,39 +24,44 @@ def fahrenheit_to_kelvin(fahrenheit):
def kelvin_to_fahrenheit(kelvin):
return (kelvin - 273.15) * 9 / 5 + 32

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)

}
Comment on lines +27 to +35
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)
]


def main():
print("🌡️ Temperature Converter")
print("1. Celsius to Fahrenheit")
print("2. Fahrenheit to Celsius")
print("3. Celsius to Kelvin")
print("4. Kelvin to Celsius")
print("5. Fahrenheit to Kelvin")
print("6. Kelvin to Fahrenheit")

choice = input("Enter choice (1–6): ")

if choice == "1":
c = float(input("Enter Celsius: "))
print(f"{c}°C = {celsius_to_fahrenheit(c):.2f}°F")
elif choice == "2":
f = float(input("Enter Fahrenheit: "))
print(f"{f}°F = {fahrenheit_to_celsius(f):.2f}°C")
elif choice == "3":
c = float(input("Enter Celsius: "))
print(f"{c}°C = {celsius_to_kelvin(c):.2f}K")
elif choice == "4":
k = float(input("Enter Kelvin: "))
print(f"{k}K = {kelvin_to_celsius(k):.2f}°C")
elif choice == "5":
f = float(input("Enter Fahrenheit: "))
print(f"{f}°F = {fahrenheit_to_kelvin(f):.2f}K")
elif choice == "6":
k = float(input("Enter Kelvin: "))
print(f"{k}K = {kelvin_to_fahrenheit(k):.2f}°F")
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)]))


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
Comment on lines +41 to +64
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.")


if __name__ == "__main__":
main()