Python Forum
Post IF Statement Input - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Post IF Statement Input (/thread-33868.html)



Post IF Statement Input - OrphanedSoul - Jun-04-2021

So i've been tasked with:

Write a Python program that does the following:
1. Ask the use if he/she wants to convert from Fahrenheit to Celsius or Celsius to
Fahrenheit.

2. If the user wants to convert from Fahrenheit to Celsius
a. Ask the user to enter the Fahrenheit temperature
b. Calculate the Celsius equivalent using formula b above
c. Print the Celsius equivalent with 3 places to the right of the decimal

3. If the user wants to convert from Celsius to Fahrenheit
a. Ask the user to enter the Celsius temperature
b. Calculate the Fahrenheit equivalent using formula a above
c. Print the Fahrenheit equivalent with 3 places to the right of the decimal

MY CODE IS:

print("Option 1: Convert Fahrenheit to Celsius")
print("Option 2: Convert Celsius to Fahrenheit")
choice = str(input("Which do you want to do?    Option "))
if choice == 1:
    fahrenheit = int(input("Enter the temp in Fahrenheit: "))
    print("The Celsius equivalent is: ", (fahrenheit - 32)*(5/9))
if choice == 2:
    celsius = int(input("Enter the temp in celsius: "))
    print("The Farhenheit equivalent is: ", (celsius*9/5)+32)
OUTPUT:
Output:
Option 1: Convert Fahrenheit to Celsius Option 2: Convert Celsius to Fahrenheit Which do you want to do? Option 1 PS C:\Users\AdamC\Desktop\VS Code>
my code isn't populating the input string after the if statements. I'm completely new to coding and this is my first coding class, and i'm not finding an answer. Any help is greatly appreciated.


RE: Post IF Statement Input - OrphanedSoul - Jun-04-2021

Found it.
My option was a str and not an int. changed the first input to an int, and she works. Thanks anyways guys, i'm sure i'll be back again!

print("Option 1: Convert Fahrenheit to Celsius")
print("Option 2: Convert Celsius to Fahrenheit")
choice = int(input("Which do you want to do?    Option "))
if choice == 1:
    fahrenheit = int(input("Enter the temp in Fahrenheit: "))
    f_to_c = (fahrenheit - 32)*(5/9)
    print("The Celsius equivalent is: %4.3f" %(f_to_c))
if choice == 2:
    celsius = int(input("Enter the temp in Celcius: "))
    c_to_f = (celsius*9/5)+32
    print("The Celsius equivalent is: %4.3f" %(c_to_f))



RE: Post IF Statement Input - deanhystad - Jun-04-2021

Why convert input to an int? Are you doing math? If you don't need need a number variable why go to the extra hassle of using a number variable.

If your program converts C to F or F to C why does the user have to enter 1 or 2? Why not have the user enter F or C

The problem description mentions 2 options but your program has 3. You can convert from F to C, from C to F, or not do any conversion at all. Is that what you want?