10 Replies - 6244 Views - Last Post: 11 June 2011 - 01:33 PM Rate Topic: -----

#1 kevinja  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 29-September 10

having trouble with the temperature converter I am writing, getting so

Posted 10 June 2011 - 01:56 PM

Hi there. I am having a bit of trouble with the program I'm trying to write here in Python. I'm relatively new at the language, but I do understand a bit and know some stuff.

I've written a simple temperature converter in Python, but I get some pretty weird errors. When I choose option f when running the program in the Python interpreter, and enter a temperature value I get this error:
Traceback (most recent call last):
File "temp_convert.py", line 23, in <module>
menu()
File "temp_convert.py", line 5, in menu
ctf()
File "temp_convert.py", line 13, in ctf
temp = (c * 1.8) + 32
TypeError: can't multiply sequence by non-int of type 'float'

I understand what this means but I don't know how to tell Python I need, or rather want, to use a float. I thought numerical values can change on the fly in pYthon and not cause problems.

As for when I choose option c when running the program, I get this error, which I also don't understand:
Traceback (most recent call last):
File "temp_convert.py", line 23, in <module>
menu()
File "temp_convert.py", line 7, in menu
ftc()
File "temp_convert.py", line 19, in ftc
temp = (f -32) / 1.8
TypeError: unsupported operand type(s) for -: 'str' and 'int'

Here is all my code below. Any help you can give is greatly appreciated. Thanks in advance!

def menu():
 print "Press f to convert to Fahrenheit from Celsius, or c to convert to Celsius from Fahrenheit."
 choice=raw_input()
 if choice =="f":
  ctf()
 elif choice =="c":
  ftc()
 else:
  print "Invalid selection!"
def ctf():
 print "Enter the temperature to be converted to degrees Fahrenheit. from degrees Celsius."
 c=raw_input()
 temp = (c * 1.8) + 32
 print "The temperature in degrees Fahrenheit is %s." % temp
 menu()
def ftc():
 print "Enter the temperature to be converted to degrees Celsius from degrees Fahrenheit."
 f=raw_input()
 temp = (f -32) / 1.8
 print "The temperature in degrees Celsius is %s degrees" % temp
 menu()
print "Welcome to the temperature converter!"
menu()

This post has been edited by atraub: 10 June 2011 - 08:08 PM
Reason for edit:: Code tags ftw


Is This A Good Question/Topic? 0
  • +

Replies To: having trouble with the temperature converter I am writing, getting so

#2 Brewer  Icon User is offline

  • Awesome
  • member icon

Reputation: 179
  • View blog
  • Posts: 1,044
  • Joined: 14-June 10

Re: having trouble with the temperature converter I am writing, getting so

Posted 10 June 2011 - 01:57 PM

Please use [ code ] tags.

def menu():
	choice = raw_input("Press 'f' to convert Celsius to Fahrenheit, or 'c' to convert Fahrenheit to Celsius.")
	if choice == 'f':
		ctf()
	elif choice == 'c':
		ftc()
	else:
		print "Invalid."
		
def ctf():
	c = float(raw_input("Enter temperature to be converted to degrees Fahrenheit."))
	temp = (c * (9.0 / 5.0)) + 32
	return "The temperature in degrees Fahrenheit is %d." % temp
	
def ftc():
	f = float(raw_input("Enter temperature to be converted to degrees Celsius."))
	temp = (f - 32) * (5.0 / 9.0)
	return "The temperature in degrees Celsius is %d." % temp
	
print "Welcome to the temperature converter."
menu()


Here's what I did:

1) When you ask for variables 'c' and 'f', you are getting a String. Obviously you can't perform math operations on a String. To remedy this, we cast the variable as a float by wrapping the raw_input() function in a float() function.

2) Instead of using print() and then raw_input(), you can use raw_input("With your message type thusly.").

3) Your temperature conversion formulas might be right, but you should use decimals in place of fractions. It's a bad habit to get into and could lead to problem later on down the road.

Feel free to ask questions.

This post has been edited by Brewer: 10 June 2011 - 07:27 PM

Was This Post Helpful? 0
  • +
  • -

#3 kevinja  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 29-September 10

Re: having trouble with the temperature converter I am writing, getting so

Posted 10 June 2011 - 02:00 PM

Sorry, dunno hwat happened. All the indentations were right when I pasted them in but now they're not there.
Was This Post Helpful? 0
  • +
  • -

#4 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: having trouble with the temperature converter I am writing, getting so

Posted 10 June 2011 - 02:03 PM

Inadvertently, you are attempting to multiply a string by a floating point number. What you need to do is cast your input to an integer or floating point number first:

c = int(raw_input())
temp = (c * 1.8) + 32


This post has been edited by Motoma: 10 June 2011 - 02:03 PM

Was This Post Helpful? 0
  • +
  • -

#5 Brewer  Icon User is offline

  • Awesome
  • member icon

Reputation: 179
  • View blog
  • Posts: 1,044
  • Joined: 14-June 10

Re: having trouble with the temperature converter I am writing, getting so

Posted 10 June 2011 - 02:11 PM

I've edited my previous post, check it out!
Was This Post Helpful? 0
  • +
  • -

#6 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5800
  • View blog
  • Posts: 12,636
  • Joined: 16-October 07

Re: having trouble with the temperature converter I am writing, getting so

Posted 10 June 2011 - 04:21 PM

Please post updates, not change history.

Also, still not gonna work.
e.g.
>>> 5 / 9
0
>>> 5 / 9.0
0.55555555555555558
>>> 



You need to be clear on your floats. If it's all ints, you aren't going to get what you expect.
Was This Post Helpful? 1
  • +
  • -

#7 Brewer  Icon User is offline

  • Awesome
  • member icon

Reputation: 179
  • View blog
  • Posts: 1,044
  • Joined: 14-June 10

Re: having trouble with the temperature converter I am writing, getting so

Posted 10 June 2011 - 07:26 PM

Yes, that is a mistake that I make a lot. Thanks for catching it!
Was This Post Helpful? 0
  • +
  • -

#8 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,026
  • Joined: 15-July 08

Re: having trouble with the temperature converter I am writing, getting so

Posted 10 June 2011 - 07:33 PM

View Postbaavgai, on 10 June 2011 - 07:21 PM, said:

Please post updates, not change history.

Also, still not gonna work.
e.g.
>>> 5 / 9
0
>>> 5 / 9.0
0.55555555555555558
>>> 



You need to be clear on your floats. If it's all ints, you aren't going to get what you expect.


Only in pre-Python 3. In Python 3.0+, division defaults to floating point and you have to use the // operator to do truncating division.
Was This Post Helpful? 2
  • +
  • -

#9 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: having trouble with the temperature converter I am writing, getting so

Posted 10 June 2011 - 08:08 PM

the standard for python indentation is 4 spaces... please use at least 4 spaces, 1 makes it way too hard to read.
Was This Post Helpful? 0
  • +
  • -

#10 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5800
  • View blog
  • Posts: 12,636
  • Joined: 16-October 07

Re: having trouble with the temperature converter I am writing, getting so

Posted 11 June 2011 - 03:16 AM

View PostDogstopper, on 10 June 2011 - 10:33 PM, said:

Only in pre-Python 3...


As you appear to be the only one who uses Python 3, it's a safe bet pre-Python 3 issues are in effect. :P

For indents, the number 4 is given as a recommendation. However, Python only requires consistency. Personally, I gave up a long time ago and just use tabs. I know that drives some people nuts. Deal.
Was This Post Helpful? 0
  • +
  • -

#11 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: having trouble with the temperature converter I am writing, getting so

Posted 11 June 2011 - 01:33 PM

I use python 3.2 :D


when I'm not working...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1