python 2. to 3

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2009
Posts: 19
Reputation: simpatar is an unknown quantity at this point 
Solved Threads: 0
simpatar simpatar is offline Offline
Newbie Poster

python 2. to 3

 
0
  #1
Oct 20th, 2009
I've got a book for python 2. in school and the problem is that we're programming in 3. which makes it difficult from time to time to get it right, cause you cant just copy the text in the book to the shell.

I'm stuck with this ex:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# This is a program to convert celsius to Fahrenheit

def main():
celsius = input('What is the temperature in celsius? ')
fahrenheit = (9.0 / 5.0) * celsius + 32
print('The temperature is', fahrenheit, 'degrees Fahrenheit.')

main()

~~~~~~~~~~~~~~~~~~~~~~~~~~~~

when i run it in shell for 3. i get the following error msg:

"Can't convert 'int' object to str implicitly"


Now i need help to translate this to 3. python.


Best regards
Simon
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 50
Reputation: lukerobi is an unknown quantity at this point 
Solved Threads: 16
lukerobi lukerobi is offline Offline
Junior Poster in Training
 
0
  #2
Oct 20th, 2009
try:
  1. print('The temperature is %f degrees Fahrenheit.' % fahenheit)
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,113
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 944
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite
 
1
  #3
Oct 20th, 2009
The function input() in Python3 replaces the old Python2 function raw_input() and returns a string,so you have to use int() to convert the string to an integer value ...
  1. def main():
  2. celsius = int( input("What is the temperature in celsius? ") )
  3. fahrenheit = (9.0 / 5.0) * celsius + 32
  4. print('The temperature is', fahrenheit, 'degrees Fahrenheit.')
  5.  
  6. main()
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 19
Reputation: simpatar is an unknown quantity at this point 
Solved Threads: 0
simpatar simpatar is offline Offline
Newbie Poster
 
0
  #4
Oct 20th, 2009
Thank you for the quick answers!

The last post made it, from vega~. The thing is that i want it to work with non-int(I think it's non-int, a complete rookie with programming, decimal number) numbers too. then

"celsius = int( input("What is the temperature in celsius? ") )" wont do it.
right? *confused*

So the question is, how do i make it to work with decimail numbers too?
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,113
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 944
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite
 
0
  #5
Oct 20th, 2009
Simply use float() instead of int() now you have numbers covered more generally.

Recommended book ...
"Dive Into Python"
Mark Pilgrim's Free online book, novice to pro, updated constantly.
rewritten for Python3 (check appendix A for the 2to3 differences)
http://diveintopython3.org/
Last edited by vegaseat; Oct 20th, 2009 at 10:49 am.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 50
Reputation: lukerobi is an unknown quantity at this point 
Solved Threads: 16
lukerobi lukerobi is offline Offline
Junior Poster in Training
 
0
  #6
Oct 20th, 2009
Try:
  1. fahrenheit = (9.0 / 5.0) * int(celsius) + 32

Unfortunatly that was going to be my first suggestion, but i was unsure about input only taking strings... i only use 2.6
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 19
Reputation: simpatar is an unknown quantity at this point 
Solved Threads: 0
simpatar simpatar is offline Offline
Newbie Poster
 
0
  #7
Oct 20th, 2009
Thanks alot. Float is the thing

And thank you for the tip vegaseat!
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,221
Reputation: bumsfeld will become famous soon enough bumsfeld will become famous soon enough 
Solved Threads: 138
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Nearly a Posting Virtuoso
 
0
  #8
Oct 20th, 2009
Also with Python3 you don't have to use
fahrenheit = (9.0 / 5.0) * celsius + 32
you can just use
fahrenheit = (9 / 5) * celsius + 32
since '/' is now floating point division and '//' is integer division.
Should you find Irony, you can keep her!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC