Author

Topic: Learning python Updated (Read 567 times)

legendary
Activity: 1176
Merit: 1280
May Bitcoin be touched by his Noodly Appendage
August 22, 2013, 04: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
legendary
Activity: 1018
Merit: 1000
August 22, 2013, 03: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
legendary
Activity: 1176
Merit: 1280
May Bitcoin be touched by his Noodly Appendage
August 21, 2013, 09:47:58 PM
#4
fcts is a dictionary
This explains how it works: http://docs.python.org/2/tutorial/datastructures.html#dictionaries
legendary
Activity: 1018
Merit: 1000
August 21, 2013, 09:07:56 PM
#3
jackjack thanks for the input

what exactly does fcts do?
legendary
Activity: 1176
Merit: 1280
May Bitcoin be touched by his Noodly Appendage
August 21, 2013, 08:18:33 PM
#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)
legendary
Activity: 1018
Merit: 1000
August 21, 2013, 07:38:37 PM
#1
Coming from this thread https://bitcointalksearch.org/topic/learning-python-265921

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.
Jump to: