3 Replies - 259 Views - Last Post: 19 August 2014 - 06:07 PM Rate Topic: -----

#1 jgrimmie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 18-August 14

What wrong with the function?

Posted 19 August 2014 - 05:18 PM

This again is just something I am doing for my self to attempt to teach my self python. My question is "Why do the if statements inside the function always evaluate to true?". As always thank you for any help you can supply.


##crac unit reporting...

class Unit():
	def __init__(self, name="unknown", temperature=0, humitty = 0):
		self.name = name
		self.temperature = temperature
		self.humitty = humitty
		
	def sys_health(self):
		if self.temperature >= 85:
			print "Temperature is out of tolerance!"
		else:
			print "Temperature is within tolerance!"
		if self.humitty >= 60:
			print "Humitty is out of tolerance!"
		else:
			print "Humitty is within tolerance!"
			
usercontinue = True

while usercontinue == True:
	print "__________________________________________________________"
	currentUnit = raw_input("enter unit designation: ")
	currentUnitTemp = raw_input("Enter the current temperature for unit %s: " % currentUnit)
	currentUnitHumitty = raw_input("Enter the current humitty for unit %s: " % currentUnit)
	currentUnit = Unit(currentUnit, currentUnitTemp, currentUnitHumitty)
	currentUnit.sys_health()
	
	
	endContinue = raw_input("Would you like to continue? Y/n")
	if endContinue == "n":
		print "DONE...."
		break
	else:
		print "..." 



Is This A Good Question/Topic? 0
  • +

Replies To: What wrong with the function?

#2 jgrimmie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 18-August 14

Re: What wrong with the function?

Posted 19 August 2014 - 05:49 PM

I fixed this it was do to a str vs. int issue.
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3642
  • View blog
  • Posts: 12,619
  • Joined: 12-December 12

Re: What wrong with the function?

Posted 19 August 2014 - 05:50 PM

raw_input always returns a string, you need to convert these inputs into numbers; for example:
currentUnitTemp = int(currentUnitTemp)

Was This Post Helpful? 1
  • +
  • -

#4 jgrimmie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 18-August 14

Re: What wrong with the function?

Posted 19 August 2014 - 06:07 PM

View Postandrewsw, on 19 August 2014 - 05:50 PM, said:

raw_input always returns a string, you need to convert these inputs into numbers; for example:
currentUnitTemp = int(currentUnitTemp)



That's what I ended up doing to fix it, the problem now is how do I deal with user input that is not a int causing a program crash. I am researching this now, end game for this is t have it store the information in a spread sheet and or a database. While this is an over complication of an every day task I have to complete personally several times each day, I think it will make it seem more real and give me much needed motivation.

current code:

##crac unit reporting...

class Unit():
	def __init__(self, name="unknown", temperature=0, humitty = 0):
		self.name = name
		self.temperature = temperature
		self.humitty = humitty
		
	def sys_health(self):
		if self.temperature > 85:
			#print self.temperature #for debug use
			print "Temperature is out of tolerance!"
		else:
			print "Temperature is within tolerance!"
		if self.humitty > 60:
			#print self.humitty #for debug use
			print "Humitty is out of tolerance!"
		else:
			print "Humitty is within tolerance!"
			
usercontinue = True

while usercontinue == True:
	print "__________________________________________________________"
	currentUnit = str(raw_input("enter unit designation: "))
	currentUnitTemp = int(raw_input("Enter the current temperature for unit %s: " % currentUnit))
	currentUnitHumitty = int(raw_input("Enter the current humitty for unit %s: " % currentUnit))
	currentUnit = Unit(currentUnit, currentUnitTemp, currentUnitHumitty)
	currentUnit.sys_health()
	
	
	endContinue = raw_input("Would you like to continue? Y/n")
	if endContinue == "n":
		print "DONE...."
		break
	else:
		print "..." 


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1