This is my first official python program/scrip which I've done my self;) yay!:D. Im sure most of you guys know the 21 card trick where you guess what card a person is thinking of accurately.

anyway, I have trouble passing information between modules within modules and functions within functions.

this is what i have done so far but cant figure out how to get "list" from mod2.py into the main?. How would i go about doing this. And i know Ive over used the "modules" here, just wanted to get used to more structure is all:)

I know what to do, but just need help with the list thing. Please help.

-Thank you.

main.py

#'21 cards thing'
pile1=['hKing','s9','dJack','s10','dAce','c9','dQueen']
pile2=['h4','h9','dKing','hAce','sKing','cAce','cKing']
pile3=['s3','c4','c7','s5','c8','c6','s6']
import mod
def sort(name):
	if name=='pile1':
		mod.passing(pile2,pile1,pile3)
		print (list)
	if name=='pile2':
		mod.passing(pile1,pile2,pile3)
	if name=='pile3':
		mod.passing(pile2,pile3,pile1)
	
#----------Main--------------------------------------------------------------------------------	
print('Think of a card in one of the boxes')
print('\nPile1: ',pile1)
print('\nPile2: ',pile2)
print('\nPile3: ',pile3)
#--------------2nd---------------------------
print('\n\nOkay..So which  is the card your thinking of in?')
ans=input('Pile :') 

if 'pile1'in ans:
	sort(pile1)
if 'pile2'in ans:
	sort(pile2)
if 'pile3'in ans:
	sort(pile3)

mod.py

#'mod'
import mod2
def passing(t,m,b):
	if m==pile1:
		mod2.calc(pile2,m,pile3)
		return list
	if m==pile2:
		mod2.calc=(pile1,m,pile3)
	if m==pile3:
		mod2.calc=(pile2,m,pile1)

mod2.py

#'mod2'
def calc(t,m,b):
	list=t+m+b 
	return list

When you return someting from a function you should use a container to "catch" it...

returned_list = mod2.calc(pile2,m,pile3)
return returned_list

Understand what I mean?

+1 for Jlm699
Nothing new but using modules' returned value is same as functions

def addition(a, b):
    c = a+b
    return c
summ = addition(2, 3)
print summ

The only difference is, the module is imported and uses as you always access module methods
assume that is in module named math_module

import math_module as math
summ = math.addition(2, 3)
print summ

I see, but I don't understand why
"listMain" isn't printed onto the screen?, It just comes up blank?.

main.py

#'21 cards thing'
pile1=['hKing','s9','dJack','s10','dAce','c9','dQueen']
pile2=['h4','h9','dKing','hAce','sKing','cAce','cKing']
pile3=['s3','c4','c7','s5','c8','c6','s6']
import mod
def sort(name):
	if name=='pile1':
		listMain=mod.passing(pile2,pile1,pile3)
		print(listMain)
	if name=='pile2':
		mod.passing(pile1,pile2,pile3)
	if name=='pile3':
		mod.passing(pile2,pile3,pile1)

	
#----------Main--------------------------------------------------------------------------------	
print('Think of a card in one of the boxes')
print('\nPile1: ',pile1)
print('\nPile2: ',pile2)
print('\nPile3: ',pile3)
#--------------2nd---------------------------
print('\n\nOkay..So which  is the card your thinking of in?')
ans=input('Pile :') 

if 'pile1'in ans:
	sort(pile1)
if 'pile2'in ans:
	sort(pile2)
if 'pile3'in ans:
	sort(pile3)

mod.py

#'mod'
import mod2
def passing(t,m,b):
	if m==pile1:
		listMod1=mod2.calc(pile2,m,pile3)
		return listMod1
	if m==pile2:
		mod2.calc=(pile1,m,pile3)
	if m==pile3:
		mod2.calc=(pile2,m,pile1)

mod2.py
#'mod2'

def calc(t,m,b):
	listMod2=t+m+b 
	return listMod2

The only place in your code where you attempt to print listMain is from inside sort, if the name passed to sort is 'pile1', right?

When do you ever pass the name 'pile1' to sort?

(You pass the list that is named pile1 to sort, but it is not equal to 'pile1' it is equal to whatever is currently in the list pile1.)

PS- When posting python code, please use the python language code tags:
[code=Python] # Your code here

[/code]

The only place in your code where you attempt to print listMain is from inside sort, if the name passed to sort is 'pile1', right?

When do you ever pass the name 'pile1' to sort?

(You pass the list that is named pile1 to sort, but it is not equal to 'pile1' it is equal to whatever is currently in the list pile1.)

PS- When posting python code, please use the python language code tags:
[code=Python] # Your code here

[/code]

sorry but i do not understand...:S . How do i fix it?. Can you give me an example.

-thank you

From your code, when you call sort(name) :

if 'pile1'in ans:
	sort(pile1)
if 'pile2'in ans:
	sort(pile2)
if 'pile3'in ans:
	sort(pile3)

You pass in one of the lists, pile1, pile2 or pile3.

Then inside sort, you are comparing what you were passed with the strings 'pile1', 'pile2' or 'pile3'.

def sort(name):
    if name=='pile1':
        listMain=mod.passing(pile2,pile1,pile3)
        print(listMain)
    if name=='pile2':
        mod.passing(pile1,pile2,pile3)
    if name=='pile3':
        mod.passing(pile2,pile3,pile1)

In your code as written, none of the tests will ever be true. If you changed those tests to be like the tests in mod.passing (why you have mod still isn't really clear) should work (even though they are entirely un-necesary).

In mod.passing, you compare the argument with the value, not with a string:

def passing(t,m,b):
    if m==pile1:
        listMod1=mod2.calc(pile2,m,pile3)
        return listMod1
    if m==pile2:
        mod2.calc=(pile1,m,pile3)
    if m==pile3:
        mod2.calc=(pile2,m,pile1)

The reason I said the tests were un-necessary is that you could replace all of the code in passing(t,m,b) with:

def passing(t,m,b):
    return mod2.calc(t,m,b)

(I don't see any purpose for mod2 either unless calling between mods is the purpose.)

You could actually simplify the whole bit by changing sort to take 3 parameters as well and making the decision as to what order to pass the 3 in from your main code:

def sort(t,m,b):
    return mod.passing(t,m,b)
# then down in your main code:
if 'pile1'in ans:
    sort(pile2, pile1, pile3)
if 'pile2'in ans:
    sort(pile1, pile2, pile3)
if 'pile3'in ans:
    sort(pile2, pile3, pile1)

But none of that code ever does anything with the return value, so to 'see' what happened, use the following main code:

if 'pile1'in ans:
    listMain = sort(pile2, pile1, pile3)
if 'pile2'in ans:
    listMain = sort(pile1, pile2, pile3)
if 'pile3'in ans:
    listMain = sort(pile2, pile3, pile1)
print("DEBUG: ListMain:", listMain)

Starting the testing I noticed that you appear to be using Python 3.0 and I still have Python 2.5.2 installed and I'm not ready to upgrade yet so I had to make changes to the print and input statements in my code.

Here's some sample run output:

Prompt >cards.py
Think of a card in one of the boxes

Pile1:  ['hKing', 's9', 'dJack', 's10', 'dAce', 'c9', 'dQueen']

Pile2:  ['h4', 'h9', 'dKing', 'hAce', 'sKing', 'cAce', 'cKing']

Pile3:  ['s3', 'c4', 'c7', 's5', 'c8', 'c6', 's6']


Okay..So which  is the card your thinking of in?
Pile :pile1
DEBUG: ListMain: ['h4', 'h9', 'dKing', 'hAce', 'sKing', 'cAce', 'cKing', 'hKing'
, 's9', 'dJack', 's10', 'dAce', 'c9', 'dQueen', 's3', 'c4', 'c7', 's5', 'c8', 'c
6', 's6']

Note that input handling has a long way to go still:

Prompt >cards.py
Think of a card in one of the boxes

Pile1:  ['hKing', 's9', 'dJack', 's10', 'dAce', 'c9', 'dQueen']

Pile2:  ['h4', 'h9', 'dKing', 'hAce', 'sKing', 'cAce', 'cKing']

Pile3:  ['s3', 'c4', 'c7', 's5', 'c8', 'c6', 's6']


Okay..So which  is the card your thinking of in?
Pile :Pile3
DEBUG: ListMain:
Traceback (most recent call last):
  File "cards.py", line 25, in <module>
    print "DEBUG: ListMain:", listMain
NameError: name 'listMain' is not defined

I hope that's clear enough, I still don't believe in handing you completed solutions, you need to put the parts together yourself so you understand what you did.

thanks for taking the time murtan. I am now re-writing it. One more question though.

list=['one','two','three','four','five','six']

group1=list[0],list[1]
group2=list[2],list[3]
group3=list[4],list[5]

print (group1,group2,group3)

how would i do the same without having to write 'list' after

group1=list[0],list[1],list[2] #etc

each time?.

-thanks

I'm not quite sure what you're trying to ask for, but did you know you can take a slice of a list?

For example, this produces almost the same output as what you had before.

list=['one','two','three','four','five','six']

group1 = list[0:2]
group2 = list[2:4]
group3 = list[4:]

print (group1,group2,group3)

Another fun thing to play with are list comprehensions:
(The comments are the result from the previous line)

[list[x] for x in [1,3,5]]
#['two', 'four', 'six']

[list[x] for x in range(0,len(list),2)]
#['one', 'three', 'five']

[list[x] for x in range(1,len(list),2)]
#['two', 'four', 'six']

[list[x] for x in [1,3,4]]
#['two', 'four', 'five']

Is any of that useful?
(If not, try to explain more what you're looking for.)

nono what im trying to do is, separte the contents of my listMain into different groups.
suppose it was

listMain=['one','two','three','four','five','six','seven','eight','nine','ten']

and i wanted it to be

group1=listMain[0],listMain[4],listMain[9]
group2=listMain[3],listMain[7],listMain[6]

so its not in order and i cant take a slice. Is there a way in which i can write the whole thing without writing 'listMain' after each ',' so as to save time?

That's what the comprehensions were for:

group1 = [listMain[x] for x in [0,4,9]]
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.