944,093 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 504
  • Python RSS
Oct 20th, 2009
0

python 2. to 3

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
simpatar is offline Offline
23 posts
since Oct 2009
Oct 20th, 2009
0
Re: python 2. to 3
try:
Python Syntax (Toggle Plain Text)
  1. print('The temperature is %f degrees Fahrenheit.' % fahenheit)
Reputation Points: 14
Solved Threads: 16
Junior Poster in Training
lukerobi is offline Offline
50 posts
since Sep 2009
Oct 20th, 2009
1
Re: python 2. to 3
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 ...
python Syntax (Toggle Plain Text)
  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()
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Oct 20th, 2009
0
Re: python 2. to 3
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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
simpatar is offline Offline
23 posts
since Oct 2009
Oct 20th, 2009
0
Re: python 2. to 3
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.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Oct 20th, 2009
0
Re: python 2. to 3
Try:
Python Syntax (Toggle Plain Text)
  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
Reputation Points: 14
Solved Threads: 16
Junior Poster in Training
lukerobi is offline Offline
50 posts
since Sep 2009
Oct 20th, 2009
0
Re: python 2. to 3
Thanks alot. Float is the thing

And thank you for the tip vegaseat!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
simpatar is offline Offline
23 posts
since Oct 2009
Oct 20th, 2009
0
Re: python 2. to 3
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.
Reputation Points: 404
Solved Threads: 180
Nearly a Posting Virtuoso
bumsfeld is offline Offline
1,422 posts
since Jul 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Cutting a string in equal pieces
Next Thread in Python Forum Timeline: Problems writing code for my project





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC