| | |
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
alarm assignment avogadro beginner bluetooth character cmd code copy customdialog cx-freeze data decimals dictionary directory dynamic error examples excel exe file float format function generator gnu graphics gui halp homework http ideas import input itunes java leftmouse line linux list lists logging loop maze module mouse number numbers output parsing path port prime program programming projects push py2exe pygame pyglet pyqt python random recursion schedule screensaverloopinactive script scrolledtext slicenotation sqlite ssh stdout string strings sudokusolver table terminal text thread threading time tkinter tlapse tuple tutorial ubuntu unicode update urllib urllib2 variable ventrilo verify vigenere web webservice wikipedia windows wxpython xlib






