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)
-
why do you use 'split'?freude– freude2014年11月30日 13:19:56 +00:00Commented 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?kroneckersdelta– kroneckersdelta2014年11月30日 13:22:49 +00:00Commented Nov 30, 2014 at 13:22
2 Answers 2
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.
2 Comments
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)