Author

Topic: Programming in Python (Read 2698 times)

vip
Activity: 756
Merit: 503
October 14, 2013, 12:26:28 PM
#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, 12:02:36 PM
#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, 04: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, 03: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, 06: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, 06: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, 06: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, 06: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, 05:37:38 PM
#50
I need to learn python
vip
Activity: 756
Merit: 503
hero member
Activity: 812
Merit: 1000
October 13, 2013, 04: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, 03: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, 01: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, 01: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, 01: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, 01: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, 01: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, 01: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, 01: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, 12:58:29 PM
#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())
vip
Activity: 756
Merit: 503
October 13, 2013, 12:51:26 PM
#38
And that's the problem. Your toy Python interpreter did not catch the problem.
I know it's meant to make it easy for student to share the link for their homework but the python implementation lack some feature.
hero member
Activity: 812
Merit: 1000
October 13, 2013, 12:50:35 PM
#37
The problem is not with the functions. This is pointless:

Code:
def miles_to_km_with_float(miles):
    return float(miles) * 1.60934

vip
Activity: 756
Merit: 503
October 13, 2013, 12:49:08 PM
#36
Everything I said is correct.

You're writing bad code. Here is a fixed version of your code:

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

def miles_to_km_with_float(miles):
    return float(miles) * 1.60934

miles_input=input('Please input miles to convert in km:')
print str(miles_input) + " miles = " + str(miles_to_km_no_float(miles_input)) + " km (no float)"

print str(miles_input) + " miles = " + str(miles_to_km_with_float(miles_input)) + " km (float)"

It works fine using python on my computer but still get the same old result with codeskulptor.
hero member
Activity: 812
Merit: 1000
October 13, 2013, 12:48:25 PM
#35
And that's the problem. Your toy Python interpreter did not catch the problem.
vip
Activity: 756
Merit: 503
October 13, 2013, 12:46:48 PM
#34
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'
hero member
Activity: 812
Merit: 1000
October 13, 2013, 12:46:37 PM
#33
Everything I said is correct.

You're writing bad code. Here is a fixed version of your code:

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

def miles_to_km_with_float(miles):
    return float(miles) * 1.60934

miles_input=input('Please input miles to convert in km:')
print str(miles_input) + " miles = " + str(miles_to_km_no_float(miles_input)) + " km (no float)"

print str(miles_input) + " miles = " + str(miles_to_km_with_float(miles_input)) + " km (float)"
hero member
Activity: 812
Merit: 1000
October 13, 2013, 12:41:40 PM
#32
I'm pretty sure I know what your problem is. First, what version of Python are you using. Type python at the command prompt.
vip
Activity: 756
Merit: 503
October 13, 2013, 12:30:42 PM
#31
An integer multiplied by a float returns a float. Since 1.60934 is a float, even if miles is an integer, the result will be a float.

If miles == 1 with this code:
Code:
def miles_to_km(miles):
    return miles * 1.60934

The answer is "11"  Huh

If use this line:

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

The answer is correct.

This program prints 1.60934.

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

print miles_to_km(1)

Show me your entire program here.

http://www.codeskulptor.org/#user19_9Zu5nZ2ctm_3.py

I think it's because that I convert a string to a float. It is not integer --> float.
hero member
Activity: 812
Merit: 1000
October 13, 2013, 12:27:11 PM
#30
An integer multiplied by a float returns a float. Since 1.60934 is a float, even if miles is an integer, the result will be a float.

If miles == 1 with this code:
Code:
def miles_to_km(miles):
    return miles * 1.60934

The answer is "11"  Huh

If use this line:

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

The answer is correct.

This program prints 1.60934.

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

print miles_to_km(1)

Show me your entire program here.
vip
Activity: 756
Merit: 503
October 13, 2013, 12:24:15 PM
#29
An integer multiplied by a float returns a float. Since 1.60934 is a float, even if miles is an integer, the result will be a float.

If miles == 1 with this code:
Code:
def miles_to_km(miles):
    return miles * 1.60934

It prints "11"  Huh

If use this line:

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

The answer is correct.
hero member
Activity: 812
Merit: 1000
October 13, 2013, 12:22:19 PM
#28
An integer multiplied by a float returns a float. Since 1.60934 is a float, even if miles is an integer, the result will be a float.
vip
Activity: 756
Merit: 503
October 13, 2013, 11:33:37 AM
#27
Nice  Smiley

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

def km_to_miles(km):
    return km / 1.60934

print miles_to_km(10)
print km_to_miles(16.0934)

Edit:
Improved version that assumes that the user only input numbers:

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

def km_to_miles(km):
    return float(km) / 1.60934

miles_input = input('Please input miles to convert in km:')
print miles_input + " miles = " + str(miles_to_km(miles_input)) + " km"

km_input = input('Please input km to convert in miles:')
print km_input + " km = " + str(km_to_miles(km_input)) + " miles"

hero member
Activity: 812
Merit: 1000
October 13, 2013, 11:27:12 AM
#26
Working on the functions lesson at the moment, I didn't really understand it at first. I think I've got it now as I tried to write some of my own and it outputs the correct answer. Maybe someone can check that my thinking is right on these functions I wrote.

Quote
# converts cm to inches

def cm_to_inches(cm):
    inch = 1 / 2.54 * cm
    return inch

a1 = cm_to_inches(25.4)
print a1

Less lines are better when it doesn't reduce clarity. The reason is it allows more lines visible on the screen, which allows greater comprehension while examining the code. But don't sacrifice white space to achieve that.

Quote
def cm_to_inches(cm):
    return cm / 2.54

And there's no point in dividing by 1 or multiplying by 1 or zero when they are constants:

Quote
# convert inches to cm

def inch_to_cm(inch):
    cm = 2.54 / 1 * inch
    return cm

Try this:

Quote
def inch_to_cm(inch):
    return inch * 2.54
legendary
Activity: 1120
Merit: 1038
October 13, 2013, 06:53:49 AM
#25
I recommend trying a guide called learn python the hard way.
It is really helpful and easy.
vip
Activity: 756
Merit: 503
October 12, 2013, 11:47:26 PM
#24
Is this about right for creating a function and then creating another which calls the previous function?

http://www.codeskulptor.org/#user19_v3vzRwYgMq_1.py
You can call a function anywhere you want in your code, even inside another function.
newbie
Activity: 28
Merit: 0
October 12, 2013, 11:39:04 PM
#23
Is this about right for creating a function and then creating another which calls the previous function?

http://www.codeskulptor.org/#user19_v3vzRwYgMq_1.py
newbie
Activity: 28
Merit: 0
October 12, 2013, 10:23:32 PM
#22
Yep you got it
Just two remarks: do you know you can "print cm_to_inches(1.0)" directly? And why writing ones in the formulas? Why not just "cm=2.54*inch"?

The example used in the course is fahrenheit to celsius but I couldn't seem to grasp it. I tried thinking of a few different ways I might use a function and those are just the things that came to mind. Obviously they are not useful as you've provided better options but for the purposes of understanding how a function operates, it seems to have worked for me Smiley
vip
Activity: 756
Merit: 503
October 12, 2013, 10:22:32 PM
#21
By the way there is a computer networks course starting in 3 months: https://www.coursera.org/course/comnetworks

Quote
Course Syllabus:
Introduction, Protocols and Layering
Physical and Link layers
Retransmissions, Multiple access, Switching
Network layer, Internetworking
Intra- and Inter-domain Routing
Transport layer, Reliability
Congestion Control
DNS, Web/HTTP, Content Distribution
Quality of Service and Real-time Apps
Network Security
vip
Activity: 756
Merit: 503
October 12, 2013, 10:18:04 PM
#20
do you know you can "print cm_to_inches(1.0)" directly?
You are right but he did it the way it is taught in the video. I guess it is easier for beginner to understand.
vip
Activity: 756
Merit: 503
October 12, 2013, 10:15:51 PM
#19
You can share your code with CodeSkulpor for easier review: http://www.codeskulptor.org/#user19_HWWqVRQDa8_0.py
I don't know the exact formula but the code looks good to me.

I just finished everything for this week.  Cheesy
legendary
Activity: 1176
Merit: 1280
May Bitcoin be touched by his Noodly Appendage
October 12, 2013, 10:12:57 PM
#18
Yep you got it
Just two remarks: do you know you can "print cm_to_inches(1.0)" directly? And why writing ones in the formulas? Why not just "cm=2.54*inch"?
newbie
Activity: 28
Merit: 0
October 12, 2013, 10:04:36 PM
#17
Working on the functions lesson at the moment, I didn't really understand it at first. I think I've got it now as I tried to write some of my own and it outputs the correct answer. Maybe someone can check that my thinking is right on these functions I wrote.

Quote

# converts cm to inches

def cm_to_inches(cm):
    inch = 1 / 2.54 * cm
    return inch

a1 = cm_to_inches(25.4)
print a1



# convert inches to cm

def inch_to_cm(inch):
    cm = 2.54 / 1 * inch
    return cm

a2 = inch_to_cm(10)
print a2



# convert 100 to 1 (100 = 1, 500 = 5, 1000 = 10)

def onehundred_to_one(hundred):
    one = hundred / 100
    return one

a3 = onehundred_to_one(800)
print a3


# convert 1 to 100 (1 = 100, 5 = 500, 10 = 1000)

def one_to_onehundred(one):
    hundred = 100 * one
    return hundred

a4 = one_to_onehundred(5)
print a4
newbie
Activity: 28
Merit: 0
October 12, 2013, 05:09:57 AM
#16
New lessons are up.
staff
Activity: 3290
Merit: 4114
October 10, 2013, 04:49:28 PM
#15
Python being a Object orientated language is a great start. If you ever do get good enough that you know a lot, then you should also pick up other languages easier too.
hero member
Activity: 812
Merit: 1000
October 10, 2013, 04:40:41 PM
#14
Python is a great language, it is a lot quicker to code then any other language out there. It is not too slow compare to C either.

It's very slow compared to C. Sometimes 50 or more times slower. On the other hand, if your whole Python program just does one function call which does some serious number crunching, and that Python function was written in C, then sure, it's not too slow compared to C.

Python speed: http://benchmarksgame.alioth.debian.org/u32/benchmark.php?test=nbody&lang=python3&id=1&data=u32

C speed: http://benchmarksgame.alioth.debian.org/u32/benchmark.php?test=nbody&lang=gcc&id=1&data=u32

Results show C is 50 times faster and uses one tenth the amount of memory.
hero member
Activity: 812
Merit: 1000
October 10, 2013, 04:29:17 PM
#13
Do SICP http://mitpress.mit.edu/sicp/

Afterwards you can code in any language, you just need to learn it's syntax which takes a weekend.

Incorrect. Oh sure, you're going to learn Lisp, C++, Haskell and Prolog in a weekend just by learning its syntax.
member
Activity: 84
Merit: 10
October 10, 2013, 09:47:37 AM
#12
Python is a great language, it is a lot quicker to code then any other language out there. It is not too slow compare to C either.
hero member
Activity: 756
Merit: 500
It's all fun and games until somebody loses an eye
October 10, 2013, 09:44:04 AM
#11
I learned some python a while ago. I found it useful to solve the problems at projecteuler.net using python to experiment with different programming and mathematical techniques.
legendary
Activity: 1018
Merit: 1000
October 10, 2013, 09:37:53 AM
#10
I took learning a programming language as a hobby and chose python. It has been a great learning experience and really keeps your mind busy.

JackJack has been a great "guidance counselor"

So enjoy your ride...

Still got a lot of learning to do but its "educational entertainment"
full member
Activity: 126
Merit: 100
October 10, 2013, 01:40:11 AM
#9
Yeah, if you're looking to do web stuff python probably isn't the most "ideal," BUT python IS a great multi-purpose language. It can be used for so many things, much like C/C++.
newbie
Activity: 28
Merit: 0
October 10, 2013, 01:17:35 AM
#8
Just a reminder that this course has started (week 0). It's pretty straight forward but we can use this thread to assist one another if needed.
hero member
Activity: 899
Merit: 1002
September 28, 2013, 08:32:18 PM
#7
Do SICP http://mitpress.mit.edu/sicp/

Afterwards you can code in any language, you just need to learn it's syntax which takes a weekend.
sr. member
Activity: 840
Merit: 255
SportsIcon - Connect With Your Sports Heroes
September 28, 2013, 05:59:53 PM
#6
Instead of learning python, learn the OOP then you can easily learn any language. I personally think python is a descent web language. I personally like php, and JavaEE for web, and Objective-c for desktop/ios apps.
This is nitpicking, but python is not a "web language". It's multipurpose and one can code desktop programs with it (and wxPython). I did so, some time ago.
vip
Activity: 756
Merit: 503
September 27, 2013, 08:46:32 PM
#5
I'm now also registered to an initiation course to C++ but it's in french: https://www.coursera.org/course/intro-cpp-fr

I'm already semi-fluent in C++ but only learnt via source code reading and working on small projects. Maybe it will help me to start from the beginning, then there is a part 2 course for more advanced students.  
newbie
Activity: 28
Merit: 0
September 26, 2013, 10:01:27 PM
#4
Thanks for the offer of assistance.

I'll post this over at /r/bitcoin and see if we can get a small group for the 9th next month.
legendary
Activity: 1176
Merit: 1280
May Bitcoin be touched by his Noodly Appendage
September 26, 2013, 09:21:24 PM
#3
I don't know if this is the ideal language to learn if I want to develop my Bitcoin ideas, perhaps someone can offer some advice in that regard
I learnt Python and Bitcoin by looking/forking this software: https://github.com/joric/pywallet/
I think I'm now pretty good at both
I can help you if you need some help
vip
Activity: 756
Merit: 503
September 26, 2013, 08:22:59 PM
#2
I'm in! I think python is a good multi-purpose language.
newbie
Activity: 28
Merit: 0
September 26, 2013, 08:19:30 PM
#1
Hey,

I've been wanting to learn to program for a while but I never really bothered as I had no idea of what to build and so my motivation was zero. Because of Bitcoin, I have some simple ideas that I'd like to build so now is as good a time as any to learn how to program. To that end, I thought I would share with others a course on python is starting on October 9th titled "An Introduction to Interactive Programming in Python".

https://www.coursera.org/course/interactivepython

I don't know if this is the ideal language to learn if I want to develop my Bitcoin ideas, perhaps someone can offer some advice in that regard but anyway, I thought I would share the link here and see if anyone else was interested in doing this as a group, perhaps we can collaborate and help each other to learn via the forum or on irc in the #interactivepython channel.

Anyone else interested?





Jump to: