1

So, I am working on a small project. I am making a text-based temperature calculator but I have run into a problem with an if statement in the program, just as the title of this question suggests.

This is the code that I am having issues with.

running = True
fahr = "fahrenheit"
cel = "celsius"
kel = "kelvin"
temperatures = [fahr, cel, kel]

while running == True:
    try:
        temperature_type = str.lower(input("What is the current temperature unit you are using? \nFahrenheit, Celsius, or Kelvin?\n"))
    except ValueError:
        print("I do not understand. Please try again.")
        continue
    if temperature_type == any(temperatures) and not all(temperatures):
        break
    else:
        print("Please enter a valid input.")
        continue

It seems that there is something wrong with the logic that I am not seeing, and I have tried multiple combinations, even ones that are suggested from this post and none of them seem to work the way I want it to. The way I want it to work is to let the variable temperature_type be equal to only one of the temperatures such as Fahrenheit and then ask another question (which is not shown here). If the variable temperature_type does not equal any of the three, then I want to loop to repeat. The issue I am having is that no matter what I input, it always asks for the temperature. If anyone has an answer I would be thrilled to know what I am doing wrong and I would also love an explanation of the logic because I am not the best with this sort of logic yet. Thanks again for any answers and/or guidance!

4 Answers4

4

any and all return True or False, not some object that can be compared meaningfully to a str. All non-empty str are "truthy", so you're checking if temperature_type == True and not True, which of course will never pass.

You're desired logic would probably be:

if temperature_type in temperatures:

It's impossible for someone to enter multiple temperature types in your design, so you don't need any equivalent to the all test, you just want to know if the entered string matches one of the three known strings.

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • Thank you! This helped out a lot! I really appreciate you (as well and everyone else) for taking the time to answer my question! This makes so much more sense now! – 360programmer Aug 20 '18 at 03:13
1

Here's a simpler version of the check that simply checks that the input string is one of the valid temperatures. It uses the in operator to check that the provided string is in temperatures.

running = True
fahr = "fahrenheit"
cel = "celsius"
kel = "kelvin"
temperatures = [fahr, cel, kel]

while running:
    temperature_type = str.lower(input("What is the current temperature unit you are using? \nFahrenheit, Celsius, or Kelvin?\n"))
    if temperature_type in temperatures:
        break
    print("Please enter a valid input.")
101
  • 8,514
  • 6
  • 43
  • 69
0

OR:

while 1:
    if input("What is the current temperature unit you are using? \nFahrenheit, Celsius, or Kelvin?\n").lower() in temperatures:
        break
    print("Please enter a valid input.")

Explanation:

all and any returns True or False so it will not work so use in

I removed running because just use 1

You can do True too, see:

>>> 1==True
True
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
0

Maybe expand to include all of the scales, and make it the user interface easier.

scales = ['F', 'R', 'C', 'K']

while True:
    if input("What temperature scale are you using [F, C, K, R]? ")[0].upper() in scales: break

[0]

Fred Mitchell
  • 2,145
  • 2
  • 21
  • 29