Bitcoin Forum
May 07, 2024, 02:56:53 AM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Learning python Updated  (Read 539 times)
vite (OP)
Legendary
*
Offline Offline

Activity: 1018
Merit: 1000


View Profile
August 22, 2013, 12:38:37 AM
 #1

Coming from this thread https://bitcointalk.org/index.php?topic=265921.0

Im am still crawling like a baby when it comes to programming, but I got bored and decided to try to code something out.
And it seems to be popular to use github so I published it over there.

https://github.com/vitepython/askbid

Well, now to continue to learn.
It is a common myth that Bitcoin is ruled by a majority of miners. This is not true. Bitcoin miners "vote" on the ordering of transactions, but that's all they do. They can't vote to change the network rules.
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1715050613
Hero Member
*
Offline Offline

Posts: 1715050613

View Profile Personal Message (Offline)

Ignore
1715050613
Reply with quote  #2

1715050613
Report to moderator
1715050613
Hero Member
*
Offline Offline

Posts: 1715050613

View Profile Personal Message (Offline)

Ignore
1715050613
Reply with quote  #2

1715050613
Report to moderator
1715050613
Hero Member
*
Offline Offline

Posts: 1715050613

View Profile Personal Message (Offline)

Ignore
1715050613
Reply with quote  #2

1715050613
Report to moderator
jackjack
Legendary
*
Offline Offline

Activity: 1176
Merit: 1233


May Bitcoin be touched by his Noodly Appendage


View Profile
August 22, 2013, 01:18:33 AM
 #2

Suggestions to increase your pythonicness

0. Use this AS MUCH AS YOU CAN
Also reachable with Google: https://google.com/search?q=python+doc+types, https://google.com/search?q=python+doc+dictionary, https://google.com/search?q=python+doc+string

1. You can change
Code:
    choice = float (raw_input("Enter Choice: "))
    if choice == 1:
        usd2btc()
    if choice == 2:
        btc2usd()
to
Code:
    fcts={1:usd2btc, 2:btc2usd}
    choice = None   #Unlikely you would ever have None as a key in a dictionary
    while choice not in fcts.keys():
        choice = int (raw_input("Enter Choice: "))
    fcts[choice]()


2. No ';' needed, and here your returns are not necessary
Code:
def btc2usd():
    ...
    print "Youre new asking price should be %s" % buy
    return main();
would become
Code:
def btc2usd():
    ...
    print "Youre new asking price should be %s" % buy
    main()


3. A better way to do the loop:
Code:
def btc2usd():
    ...
    print "Youre new asking price should be %s" % buy
    #No main() at the end, but...

while True:    #This, instead of just main()
    main()



Ask if anything is unclear
BTW, buy = ((price * afee) - price) * -1 is buy=price*(1-afee)

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.
vite (OP)
Legendary
*
Offline Offline

Activity: 1018
Merit: 1000


View Profile
August 22, 2013, 02:07:56 AM
 #3

jackjack thanks for the input

what exactly does fcts do?
jackjack
Legendary
*
Offline Offline

Activity: 1176
Merit: 1233


May Bitcoin be touched by his Noodly Appendage


View Profile
August 22, 2013, 02:47:58 AM
 #4

fcts is a dictionary
This explains how it works: http://docs.python.org/2/tutorial/datastructures.html#dictionaries

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.
vite (OP)
Legendary
*
Offline Offline

Activity: 1018
Merit: 1000


View Profile
August 22, 2013, 08:50:33 PM
 #5

Did I misunderstand the point of the drill #3 from http://learnpythonthehardway.org/book/ex13.html

I can't figure out how to get it to work.

http://pastebin.com/CE3PfLF1
jackjack
Legendary
*
Offline Offline

Activity: 1176
Merit: 1233


May Bitcoin be touched by his Noodly Appendage


View Profile
August 22, 2013, 09:19:45 PM
 #6

I'm not sure I understand it either...

You should understand why your script crashes though:
Code:
baby = raw_input("Baby is behaving? ")
mother = raw_input("Mom if feeling? ")
dad = raw_input("dad's mood is? ")
brother = raw_input("brother's is? ")
 
from sys import argv
baby, mother, dad, brother = argv

First, it's not why it crashes but you may misunderstand things:
You first define baby,... with raw_inputs
But then the "baby,mother,...=argv" redefine them as if the first four lines didn't exist

Second, the "variables separated by ',' = list" command assigns the items in the list to the variables
This REQUIRES to have as many variables as items in the list
Thus, you NEED to have 4 items in argv
As sys.argv[0] is the name of your script, you need to call your script with 3 arguments
Example:
Code:
python script.py a b c

Problem
You will see
Code:
The baby is: script.py
The mom is: a
...

Solution
Put 4 arguments in the call (thus argv will have 5 items)
And either
Code:
_, baby, mother, dad, brother = argv
or
Code:
baby, mother, dad, brother = argv[1:]



As for the meaning of the drill...
Maybe they want you to write a script that needs exactly 6 values, and if there are not enough arguments passed through the call then you should ask them with raw_inputs

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.
Pages: [1]
  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!