Bitcoin Forum
June 28, 2024, 02:02:41 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: « 1 2 [3]  All
  Print  
Author Topic: Programming in Python  (Read 2660 times)
FirstAscent
Hero Member
*****
Offline Offline

Activity: 812
Merit: 1000


View Profile
October 13, 2013, 05: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.
01BTC10
VIP
Hero Member
*
Offline Offline

Activity: 756
Merit: 503



View Profile
October 13, 2013, 05: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.   
FirstAscent
Hero Member
*****
Offline Offline

Activity: 812
Merit: 1000


View Profile
October 13, 2013, 05: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)
FirstAscent
Hero Member
*****
Offline Offline

Activity: 812
Merit: 1000


View Profile
October 13, 2013, 05: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))
01BTC10
VIP
Hero Member
*
Offline Offline

Activity: 756
Merit: 503



View Profile
October 13, 2013, 05: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.
FirstAscent
Hero Member
*****
Offline Offline

Activity: 812
Merit: 1000


View Profile
October 13, 2013, 05: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.
Peter Lambert
Hero Member
*****
Offline Offline

Activity: 756
Merit: 500

It's all fun and games until somebody loses an eye


View Profile
October 13, 2013, 07: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 <module>
    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))

Use CoinBR to trade bitcoin stocks: CoinBR.com

The best place for betting with bitcoin: BitBet.us
FirstAscent
Hero Member
*****
Offline Offline

Activity: 812
Merit: 1000


View Profile
October 13, 2013, 08: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 <module>
    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.
01BTC10
VIP
Hero Member
*
Offline Offline

Activity: 756
Merit: 503



View Profile
October 13, 2013, 08:25:17 PM
 #49

Nice one.

http://en.wikipedia.org/wiki/Printf_format_string#1990s:_PHP.2C_Python
MakeBelieve
Hero Member
*****
Offline Offline

Activity: 602
Merit: 500


View Profile
October 13, 2013, 09:37:38 PM
 #50

I need to learn python

On a mission to make Bitcointalk.org Marketplace a safer place to Buy/Sell/Trade
gangplank (OP)
Newbie
*
Offline Offline

Activity: 28
Merit: 0


View Profile
October 13, 2013, 10: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.
gangplank (OP)
Newbie
*
Offline Offline

Activity: 28
Merit: 0


View Profile
October 13, 2013, 10: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.
MakeBelieve
Hero Member
*****
Offline Offline

Activity: 602
Merit: 500


View Profile
October 13, 2013, 10: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.

On a mission to make Bitcointalk.org Marketplace a safer place to Buy/Sell/Trade
Baitty
Hero Member
*****
Offline Offline

Activity: 532
Merit: 500

Currently held as collateral by monbux


View Profile
October 13, 2013, 10:42:54 PM
 #54

There was a thread on this forum with different things people created with python.

Currently held as collateral by monbux
jackjack
Legendary
*
Offline Offline

Activity: 1176
Merit: 1255


May Bitcoin be touched by his Noodly Appendage


View Profile
October 14, 2013, 07:29:11 AM
Last edit: October 14, 2013, 09:30:12 AM by jackjack
 #55

Traceback (most recent call last):
  File "test.py", line 13, in <module>
    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)"

Own address: 19QkqAza7BHFTuoz9N8UQkryP4E9jHo4N3 - Pywallet support: 1AQDfx22pKGgXnUZFL1e4UKos3QqvRzNh5 - Bitcointalk++ script support: 1Pxeccscj1ygseTdSV1qUqQCanp2B2NMM2
Pywallet: instructions. Encrypted wallet support, export/import keys/addresses, backup wallets, export/import CSV data from/into wallet, merge wallets, delete/import addresses and transactions, recover altcoins sent to bitcoin addresses, sign/verify messages and files with Bitcoin addresses, recover deleted wallets, etc.
MakeBelieve
Hero Member
*****
Offline Offline

Activity: 602
Merit: 500


View Profile
October 14, 2013, 08:41:14 AM
 #56

I've started the course once again.

On a mission to make Bitcointalk.org Marketplace a safer place to Buy/Sell/Trade
FirstAscent
Hero Member
*****
Offline Offline

Activity: 812
Merit: 1000


View Profile
October 14, 2013, 04:02:36 PM
 #57

Traceback (most recent call last):
  File "test.py", line 13, in <module>
    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.
01BTC10
VIP
Hero Member
*
Offline Offline

Activity: 756
Merit: 503



View Profile
October 14, 2013, 04: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
Pages: « 1 2 [3]  All
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!