0

I have this, what do I need to implement to make it ask me which conversion I want?

def user_choice(Celsius to Fahrenheit, Fahrenheit to Celsius):
    return input ('Which conversion do you want to do? ')

I've tried a lot of combinations but can't seem to fit it right

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • 2
    Welcome to Stack Overflow. Please read about [asking questions](https://stackoverflow.com/help/asking). – Jason Aller Jan 18 '23 at 19:38
  • 1
    What are `Celsius to Fahrenheit` and `Fahrenheit to Celsius` supposed to be? As it stands, this is invalid syntax. – 0x5453 Jan 18 '23 at 19:43
  • [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – wwii Jan 18 '23 at 22:08

2 Answers2

2

I need more info about the vars. But you first need to name to var, to make it usable, and put something like:

Var1 = input('Which conversion do you want to do? (Press A for Cº or B for Fº)')

then some conditional like:

if Var1 == "A":

Then you do you calculations and when you finish, you do it again but with

elif Var1 == "B":

And you do your results Once you have them to print you do:

Var2 = print(Put the variable where the results are without the "")

then you return it like this

return Var2
  • Okay yes, so I have the formula for the conversion, only need to know how to make it like : The conversion c to f press A, or the conversion f to c press B then if u press a it says "enter Celsius" and then it calculates it – Charles Krylander Jan 18 '23 at 20:28
  • See the edited message (If you don't see it, is because i'm still editing) – What does women mean Jan 18 '23 at 20:30
  • @CharlesKrylander I have written the whole program with a small explanation below if you need it. – iamdeedz Jan 18 '23 at 20:39
0

So you want to ask if the user wants to convert from Celsius to Fahrenheit or Fahrenheit to Celsius. This can be done by defining a variable which stores this decision. You could store it as a number, 1 or 2. 1 could be Celsius to Fahrenheit and 2 could be Fahrenheit to Celsius. This can be done with the following code.

user_choice = input("Which conversion? Celsius to Fahrenheit = 1, Fahrenheit to Celsius = 2\n")

if user_choice == 1:  # Celsius to Fahrenheit
    # conversion code

elif user_choice == 2:  # Fahrenheit to Celsius
    # conversion code

For the conversions, you would want to take an input of what the user wants converted and then use a method for converting. This might look like this.

user_choice = input("Which conversion? Celsius to Fahrenheit = 1, Fahrenheit to Celsius = 2\n")

if user_choice == 1:  # Celsius to Fahrenheit
    user_input = input("What temperature would you like converted to Fahrenheit?\n")
    converted_temp = (user_input * 1.8) + 32
    print(f"Your converted temperature is {converted_temp}")

elif user_choice == 2:  # Fahrenheit to Celsius
    user_input = input("What temperature would you like converted to Celsius?\n")
    converted_temp = (user_input - 32) * 0.5556
    print(f"Your converted temperature is {converted_temp}")

Here, I just used the conversion formula google gave me. I hope this helps you!

EDIT: Here is the fixed code.

user_choice = int(input("Which conversion? Celsius to Fahrenheit = 1, Fahrenheit to Celsius = 2\n"))

if user_choice == 1:  # Celsius to Fahrenheit
    user_input = int(input("What temperature would you like converted to Fahrenheit?\n"))
    converted_temp = (user_input * 1.8) + 32
    print(f"Your converted temperature is {converted_temp}")

elif user_choice == 2:  # Fahrenheit to Celsius
    user_input = int(input("What temperature would you like converted to Celsius?\n"))
    converted_temp = (user_input - 32) * 0.5556
    print(f"Your converted temperature is {converted_temp}")
else:
    print("That is not a valid input.")
iamdeedz
  • 109
  • 8