sneekula 969 Nearly a Posting Maven

In python if I divide 6/3 I get 2, that's fine. But I divide 6/4 and get 1...

Why aren't I getting a decimal? I would like to know because I want to make something that can detect wether or not a number is whole. (I need help with that too). But I thought I would give it a shot myself and found that it wont give me a decimal when it should.

Thanks

Here is one way to do this:

a = 6
b = 4
# assure a float division
print a/float(b)  # 1.5
# test for integer (whole number)
print type(a) == int  # True
sneekula 969 Nearly a Posting Maven

Looks like Ghostdog is right ...

# test sample from user input
months = ['Dec', 'Jan']
# test sample from os.path.walk()
files = ['FebWork1,dat', 'JanWork3.dat', 'DecWork7.dat']  # test
dates = []
for i in files:
    for j in months:
         if j in i:
             dates.append(i)
             break
print dates
# ... or list comprehension ...
dates2 = [i for i in files for j in months if j in i  ]
print dates2
"""
my output -->
['JanWork3.dat', 'DecWork7.dat']
['JanWork3.dat', 'DecWork7.dat']
"""

I sort of can see how list comprehension evolves from the individual for loops. Is there anything list comprehension can do that one could not write out with individual for loops?

sneekula 969 Nearly a Posting Maven

I have used DrPython for a while now and really like it! Comes in Windows and Linux versions. It has clean displays and a minimum of quirks (does not like foreign characters). There are lots of configuration options. The results are displayed in an output window, and you can select and copy items from there. You can also install the code completion plugin from the internet from within the DrPython. The code completion option is great help for a beginner.

sneekula 969 Nearly a Posting Maven

Finally, it works perfectly. Thanks for all your help. What do you suggest I do now? I'm hoping to learn how to use images in my forms.

Unless you haven't done it already, I would a let the user select cities from departure and arrival listboxes, so there is no typing (and possible misspelling) needed. I have done something similar in one of my chemistry programs.

sneekula 969 Nearly a Posting Maven

What do you do to re-enable the button?

sneekula 969 Nearly a Posting Maven

In Ene's example if you code print dic['michigan'] your result would be the value 'lansing'. That is the way you access dictionaries. In other words, the key is the index.

sneekula 969 Nearly a Posting Maven

As far as I know in the US all the states and capitals are unique. Since your are entering the state and want to find that capital, Ene's solution will be the easiest.

sneekula 969 Nearly a Posting Maven

I found and played a pretty sophisticated Tic Tac Toe game written in Python at an earlier posting here:
http://www.daniweb.com/techtalkforums/thread26658.html

sneekula 969 Nearly a Posting Maven

Also a word of advice:
1) start your class names with a capital letter
2) create class instances like:
main = Main(root)
child = Child()
3) now you can get rid of the globals by associating the variables properly to the class instance via self. or the instance name.

Maybe somebody could give an example of how to pass variables between classes without using global variables.

Sorry, didn't want to highjack the thread!

sneekula 969 Nearly a Posting Maven

I agree with Jeff!
I have seen other computer language code like C, where the writer did not not use any indentation. Now you get lost in an ocean of { and } and ; which makes large code almost unreadable!

sneekula 969 Nearly a Posting Maven

I have started to program with windows concepts. It is different then console and that is were the learning curve sets in. Once you realize how to approach a GUI with its frames, windows, dialog boxes, buttons, labels and so on, you will enjoy it. I use Tkinter right now, it is part of the Python installation, and seems to be the easiest GUI toolkit to learn with.

sneekula 969 Nearly a Posting Maven

The module shutil has functions for deleting whole directory trees.

sneekula 969 Nearly a Posting Maven

If you don't know then you wont be able to help me.:confused:

Thanks for your kind answer. I took the liberty to check
http://www.pygtk.org/pygtk2reference/class-gtktreeview.html
all I can say is "ouch!", that is one complex thing! No wonder you are lost!

sneekula 969 Nearly a Posting Maven

LaMouche, nice game!

What would a dictionary do to improve it?

Would it be hard to dress the game up in a GUI like Tkinter?

sneekula 969 Nearly a Posting Maven

What is pygtk/gtk and what is is it used for?
Also, what is a treeview?

sneekula 969 Nearly a Posting Maven

Sounds like hard program to play around with. You could really do some damage to files in your hard drive!

sneekula 969 Nearly a Posting Maven

I think I will stay away from using "class" until I learn how to use OOP at least rudimentary.

sneekula 969 Nearly a Posting Maven

Just an exercise in passing arguments to functions:

def increment(a, b, c):
    a += 1
    b += 1
    c += 1
    return a, b, c

def main():
    c = d = e = 0
    c, d, e = increment(c, d, e)
    # test the result
    print c, d, e  # 1 1 1
main()
sneekula 969 Nearly a Posting Maven

I am new to Python, but it seems to me that using main(), or whatever you want to call it, forces you to write cleaner code. Here is my argument, with no main() you create automatically global variables:

def show_x():
    """possible since x is global and is not changed"""
    print x
def double_x():
    return 2 * x
x = 3
# allows for somewhat sloppy global use of x
x = double_x()
show_x()  # 6

Wrapping the last few lines into main() makes x now local:

def show_x(x):
    """there is no global x, so it has to be passed in"""
    print x
def double_x(x):
    return 2 * x
def main():
    """main makes x local and forces you to pass it properly"""
    x = 3
    x = double_x(x)
    show_x(x)  # 6
main()

It also organizes your code a little, so you don't end up with code like this, where the lines that belong into main() are scattered all over (still in sequence though):

x = 3
 
def double_x():
    return 2 * x
 
# allows for somewhat sloppy global use of x
x = double_x()
 
def show_x():
    """possible since x is global and is not changed"""
    print x
 
show_x()  # 6