Pages:
Author

Topic: Programming in Python (Read 2734 times)

vip
Activity: 756
Merit: 503
October 14, 2013, 11:26:28 AM
#58
I found another course but this one use a real interpreter Cheesy : https://www.edx.org/course/mit/6-00-1x/introduction-computer-science/1122
hero member
Activity: 812
Merit: 1000
October 14, 2013, 11:02:36 AM
#57
Traceback (most recent call last):
  File "test.py", line 13, in
    print miles_input + " miles = " + str(miles_to_km_no_float(miles_input)) + " km (no float)"
TypeError: unsupported operand type(s) for +: 'int' and 'str'
int
str

Fix
Quote
print str(miles_input) + " miles = " + str(miles_to_km_no_float(miles_input)) + " km (no float)"

It's already been done.
hero member
Activity: 602
Merit: 500
October 14, 2013, 03:41:14 AM
#56
I've started the course once again.
legendary
Activity: 1176
Merit: 1280
May Bitcoin be touched by his Noodly Appendage
October 14, 2013, 02:29:11 AM
#55
Traceback (most recent call last):
  File "test.py", line 13, in
    print miles_input + " miles = " + str(miles_to_km_no_float(miles_input)) + " km (no float)"
TypeError: unsupported operand type(s) for +: 'int' and 'str'
int
str

Fix
Quote
print str(miles_input) + " miles = " + str(miles_to_km_no_float(miles_input)) + " km (no float)"
hero member
Activity: 532
Merit: 500
Currently held as collateral by monbux
October 13, 2013, 05:42:54 PM
#54
There was a thread on this forum with different things people created with python.
hero member
Activity: 602
Merit: 500
October 13, 2013, 05:40:12 PM
#53
I need to learn python

Jump aboard with this course! I'm pretty much a beginner so we can assist one another via this thread Smiley I'm dedicating 1-2 hours a day to this, just need to make it habit for it to stick in my mind otherwise I'll forget.

Will do. Coursera seems to have other awesome courses too.
newbie
Activity: 28
Merit: 0
October 13, 2013, 05:26:39 PM
#52
So, unless you want to draw erroneous conclusions about how to program, as has become glaringly obvious in this little episode, I suggest you only test your code using your local python interpreter.

I don't know if this is related but it only uses the libraries we need to complete the course.
newbie
Activity: 28
Merit: 0
October 13, 2013, 05:20:39 PM
#51
I need to learn python

Jump aboard with this course! I'm pretty much a beginner so we can assist one another via this thread Smiley I'm dedicating 1-2 hours a day to this, just need to make it habit for it to stick in my mind otherwise I'll forget.
hero member
Activity: 602
Merit: 500
October 13, 2013, 04:37:38 PM
#50
I need to learn python
vip
Activity: 756
Merit: 503
hero member
Activity: 812
Merit: 1000
October 13, 2013, 03:09:44 PM
#48
I'm pretty sure I know what your problem is. First, what version of Python are you using. Type python at the command prompt.

There is no command prompt. It's a web browser implementation for the course. It works fine with codeskulptor but throw an error with python 2.7.3 on my computer.

Code:
Traceback (most recent call last):
  File "test.py", line 13, in
    print miles_input + " miles = " + str(miles_to_km_no_float(miles_input)) + " km (no float)"
TypeError: unsupported operand type(s) for +: 'int' and 'str'

for the print command, intead of adding a bunch of things together, it might be better to use placeholders?

Code:
print "%f miles = %f km." % (miles_input, miles_to_km_no_float(miles_input))

Yes. Such power using C style printf printing. Read up on formatting for printf.
hero member
Activity: 756
Merit: 500
It's all fun and games until somebody loses an eye
October 13, 2013, 02:57:06 PM
#47
I'm pretty sure I know what your problem is. First, what version of Python are you using. Type python at the command prompt.

There is no command prompt. It's a web browser implementation for the course. It works fine with codeskulptor but throw an error with python 2.7.3 on my computer.

Code:
Traceback (most recent call last):
  File "test.py", line 13, in
    print miles_input + " miles = " + str(miles_to_km_no_float(miles_input)) + " km (no float)"
TypeError: unsupported operand type(s) for +: 'int' and 'str'

for the print command, intead of adding a bunch of things together, it might be better to use placeholders?

Code:
print "%f miles = %f km." % (miles_input, miles_to_km_no_float(miles_input))
hero member
Activity: 812
Merit: 1000
October 13, 2013, 12:26:35 PM
#46
Code:
def miles_to_km(miles):
    return miles * 1.60934

miles_input = input('Please input miles to convert in km: ')
print miles_to_km(float(miles_input))

What's likely happening here is that when querying for input, you're getting a string. What you want is a number. You don't know if the string, when converted to a number, has a decimal place or not, so you take the safe route and convert it to a float. But this is all about preparing the arguments for the function.

The function should expect a number (int or float) and receive that.
vip
Activity: 756
Merit: 503
October 13, 2013, 12:23:20 PM
#45
Even further proof: here we're guaranteeing that we're passing an integer into the function:

Code:
def miles_to_km(miles):
    return miles * 1.60934

print miles_to_km(int(1))

Yes, I know this is how I did it the first time but it doesn't works using input() in the online version.
hero member
Activity: 812
Merit: 1000
October 13, 2013, 12:22:09 PM
#44
Even further proof: here we're guaranteeing that we're passing an integer into the function:

Code:
def miles_to_km(miles):
    return miles * 1.60934

print miles_to_km(int(1))
hero member
Activity: 812
Merit: 1000
October 13, 2013, 12:20:54 PM
#43
So, unless you want to draw erroneous conclusions about how to program, as has become glaringly obvious in this little episode, I suggest you only test your code using your local python interpreter.

I will; however, the online interpreter is the only way to submit our work for the course.   

Even so, the function was not the problem. Run this online:

Code:
def miles_to_km(miles):
    return miles * 1.60934

print miles_to_km(1)
vip
Activity: 756
Merit: 503
October 13, 2013, 12:12:13 PM
#42
So, unless you want to draw erroneous conclusions about how to program, as has become glaringly obvious in this little episode, I suggest you only test your code using your local python interpreter.

I will; however, the online interpreter is the only way to submit our work for the course.   
hero member
Activity: 812
Merit: 1000
October 13, 2013, 12:09:10 PM
#41
So, unless you want to draw erroneous conclusions about how to program, as has become glaringly obvious in this little episode, I suggest you only test your code using your local python interpreter.
vip
Activity: 756
Merit: 503
October 13, 2013, 12:07:27 PM
#40
Whatever the case, you don't need to convert your integer to a float in the function, because multiplying it by a float already does that.

Also, I can't get a program to print the Python version to work in the online version.

For example, this does not work:

import platform
print(platform.python_version())

I think it's an incomplete version 2.
hero member
Activity: 812
Merit: 1000
October 13, 2013, 11:58:29 AM
#39
Whatever the case, you don't need to convert your integer to a float in the function, because multiplying it by a float already does that.

Also, I can't get a program to print the Python version to work in the online version.

For example, this does not work:

import platform
print(platform.python_version())
Pages:
Jump to: