0

im having problems getting this follow program working as I would like it to. I have one step that is bothering me, I'll quickly go over what works first.
The program takes 2 arguments, pressure and unit, if pressure is an integer and the unit is "pascal","torr","atm" or "mbar" I want it to return "pressure=",pressure,unit. This works. If I dont enter a correct unit, I want the function to print "'unit' is not an accepted unit", this works.

Problematic part: When the variable pressure is given a float as 35.2 or a string as "test" BUT I give the variable unit a correct unit such as mbar I get the output

Enter an integer and a unit(seperated by ,): 3045.2,mbar '3045.2' is not an integer 'mbar' is not an accepted unit
Obviously this is not working like I would want it to, 'mbar' is an accepted unit. Any help would be greatly appreciated. EDIT: Im quite new to programming overall so keep that in mind please :X
The program I have written:

pressure, unit = input("Enter an integer and a unit(seperated by ,): ").split(',')
def func(pressure, unit):
    try:
        pressure=int(pressure)
    except ValueError:
        print("'"+pressure+"'" + " is not an integer")
    if(isinstance(pressure,int) and (unit == "pascal" or unit == "mbar" or unit == "atm" or unit == "torr")):
        print("pressure =",pressure,unit)
    elif(unit != "pascal" or unit != "mbar" or unit != "atm" or unit != "torr"):
        print("'"+unit+"'" + " is not an accepted unit")
func(pressure, unit)
2
  • why do you use 'split'? Commented Nov 30, 2014 at 13:19
  • No specific reason, I just wanted to have the variables 'pressure' and 'unit' assigned in one input. I could have done it with two seperate inputs, x, y =input("text").split(',') will just assign x to the first text before the ',' , and y to whatever is after the ',' . Does this somehow affect the function or was it just a general question of what split does? Commented Nov 30, 2014 at 13:22

2 Answers 2

1

If the unit is mbar but the value is a float then this check will also be False:

if(isinstance(pressure,int) and (unit == "pascal" or unit == "mbar" or unit == "atm" or unit == "torr")):

and that's why you're getting the "is not an accepted unit" message. You need to check for both things entirely separately: is the value an integer (yes/no) and is the unit valid (yes/no). If both are the case then you can print the success message. Otherwise, you'll need to print one or both failure messages independently of each other.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer. As im still quite new to programming I have a question to whats the most efficient way of doing this is;my general idea would be to use nested if statements. Is there a more another, more efficient way ? Thanks again for the fast reply
It won't make a difference in this case so you can fix the logic of your program in either way.
0

You should use else for the except clause - where it will run only if the int(pressure) run correctly. Besides, don't change type of variables; it's confusing:

def func(str_pressure, unit):
    try:
        pressure = int(str_pressure)
    except ValueError:
        print("'{}' is not an integer".format(str_pressure))
    else:
        # `pressure` is assigned
        if unit in {"pascal", "mbar", "atm", "torr"}:
            print("pressure = {} {}".format(pressure, unit))
        else:
            print("'{}' is not an accepted unit".format(unit))

pressure, unit = input("Enter an integer and a unit(seperated by ,): ").split(',')
func(pressure, unit)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.