简体   繁体   中英

How do i use lists from other functions?

Still learning python3 and im currently creating a domino game. I'm making progress so far but i haven't been working with functions at all.

My question is: How do i get the domino_stock_set into my split_stones function so i can split it up?

Seems like a dumb question but i googled half an hour and couldn't find anything that would work. I tried anything so at this point i might have several erros in the script.

import random

random_hand = random.randint(0, 7)
random_sixseven = random.randint(6,7)



def stock_set():
    global domino_stock_set
    domino_stock_set = []
    for i in range(0, 7):
        for j in range(i, 7):
            domino_stock_set.append([i, j])
    random.shuffle(domino_stock_set)
    
    print()
    print("…ᘛ⁐̤ᕐᐷ  -Stock Set-  …ᘛ⁐̤ᕐᐷ")
    print(domino_stock_set)
    print()
    return domino_stock_set



def split_stones(domino_stock_set, wanted_parts=1):
    length = len(domino_stock_set)
    return [ domino_stock_set[i*length // wanted_parts: (i+1)*length // wanted_parts] 
             for i in range(wanted_parts) ]

print("==============P L A Y E R==================")
print(split_stones(domino_stock_set, wanted_parts = random_sixseven))

print("=============C O M P U T E R===============")
print(split_stones(domino_stock_set, wanted_parts = random_sixseven))



stock_set()
split_stones()

I would just google for something like "python function arguments". Then you will get results such as eg this one .

To apply it to your example:

domino_stock_set = []
split_stones(domino_stock_set)

Furthermore, try to avoid using global variables, see eg this SO question . If you use domino_stock_set as a global variable you do not even need to pass it to any function because that variable would be available globally.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM