| | |
python 2. to 3
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Oct 2009
Posts: 19
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Sep 2009
Posts: 50
Reputation:
Solved Threads: 16
0
#2 Oct 20th, 2009
try:
Python Syntax (Toggle Plain Text)
print('The temperature is %f degrees Fahrenheit.' % fahenheit)
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 ...
python Syntax (Toggle Plain Text)
def main(): celsius = int( input("What is the temperature in celsius? ") ) fahrenheit = (9.0 / 5.0) * celsius + 32 print('The temperature is', fahrenheit, 'degrees Fahrenheit.') main()
May 'the Google' be with you!
•
•
Join Date: Oct 2009
Posts: 19
Reputation:
Solved Threads: 0
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?
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?
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/
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!
•
•
Join Date: Sep 2009
Posts: 50
Reputation:
Solved Threads: 16
0
#6 Oct 20th, 2009
Try:
Unfortunatly that was going to be my first suggestion, but i was unsure about input only taking strings... i only use 2.6
Python Syntax (Toggle Plain Text)
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
![]() |
Similar Threads
- My python program/function! (Python)
- News Story: It’s Like AJAX for Ruby and Python (Ruby)
- News Story: 450 bugs consumed by Python (Python)
- Code Snippet: Mortgage Calculator (Python) (Python)
Other Threads in the Python Forum
- Previous Thread: Binary-to-Decimal and vice-versa
- Next Thread: Problems writing code for my project
| Thread Tools | Search this Thread |
Tag cloud for Python
ansi assignment avogadro backend beginner binary bluetooth character cmd code customdialog data decimals dictionary drive dynamic error examples excel exe file float format ftp function gnu graphics gui heads homework http ideas import input java leftmouse line linux list lists logging loop module mouse newb number numbers output parsing path pointer port prime program programming progressbar projects push py2exe pygame pyqt python random recursion recursive refresh schedule screensaverloopinactive script scrolledtext sqlite ssh statistics stdout string strings sudokusolver sum table terminal text thread threading time tkinter tlapse tricks tuple tutorial ubuntu unicode update urllib urllib2 variable wikipedia windows write wxpython xlib






