-1

So I am making a yahtzee game in python. I have it setup to roll the dice when you click the button. Then you can hold the number from rolling again by clicking on it. My goal is to assign the values from this range(5) to a variable. Preferably I hope that it would update the variable every time the dice button is clicked.

This is just for a game I have been working on for myself to get better with python. I have tried to think of a way to assign it to a dict but I have been unable to find out how.

from tkinter import *
from random import randint

root = Tk()
root.title("Sam's Yahtzee")

def roll(dice, times):
    if times > 0:
        dice['text'] = randint(1, 6)
        root.after(10, roll, dice, times-1)

def roll_dices():
    for i in range(5):
        if dices[i][1].get() == 0:
            # dice is not held, so roll it
            roll(dices[i][0], 10)

dices = []
for i in range(5):
    ivar = IntVar()
    dice = Checkbutton(root, text=randint(1, 6), variable=ivar, bg='silver', bd=1, font=('Arial', 24), indicatoron=False, height=3, width=5)
    dice.grid(row=0, column=i)
    dices.append([dice, ivar])

Button(text='Dice', command=roll_dices, height=2, font=(None, 16, 'bold')).grid(row=1, column=0, columnspan=5, sticky='ew')

yahtzee = 0
threeKind = 0
fourKind = 0
fullHouse = 0
smallStraight = 0
largeStraight = 0
chance = 0

possibleHands = {"yahtzee": yahtzee,
                 "threeKind": threeKind,
                 "fourKind": fourKind,
                 "fullHouse": fullHouse,
                 "smallStraight": smallStraight,
                 "largeStraight": largeStraight,
                 "chance": chance}

root.mainloop()
  • Please be more specific about what your goal is. Do you want numbers from 0-4 in a variable as a list? – Poojan Jan 12 '19 at 00:27
  • 1
    Possible duplicate of [Python 3 turn range to a list](https://stackoverflow.com/questions/11480042/python-3-turn-range-to-a-list) – Ishaan Javali Jan 12 '19 at 00:27
  • Another very [similar question](https://stackoverflow.com/questions/33699896/python-range-to-list/33699920). [Here's a third](https://stackoverflow.com/questions/18265935/python-create-list-with-numbers-between-2-values/18266013). – Ishaan Javali Jan 12 '19 at 00:28
  • `range()` is a generator in python 3, so you would either need to call `list(range(5))` or `[*range(5)]`. – Joe Patten Jan 12 '19 at 00:31
  • @Poojan what I was hoping for was to get the same numbers from the randint(1, 6) in the CheckButton into a variable. Edit: But I still want the numbers to appear in the window –  Jan 13 '19 at 22:22
  • @ Ishaan Javali those questions helped. Thank you! –  Feb 19 '19 at 16:06

3 Answers3

1

Is this what you want?

nums = list(range(5)) #nums is now list of [0,1,2,3,4]
Poojan
  • 3,366
  • 2
  • 17
  • 33
  • I understand this, but is there a way for me to get a list of the same numbers from the for loop? –  Jan 12 '19 at 00:36
0

Another way, other than list(range(5)):

nums = [*range(5)]
print(nums)

# [0, 1, 2, 3, 4]

It seems like it's quite a bit faster, too. (I used 100 for more accurate testing.)

In [1]: %timeit nums = list(range(100))
3.24 µs ± 87.8 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [2]: %timeit nums = [*range(100)]
1.08 µs ± 40.2 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
iz_
  • 15,923
  • 3
  • 25
  • 40
0

I understand this, but is there a way for me to get a list of the same numbers from the for loop?

I guess you just want a block of statements to be executed for n(=1000 here) times, and every time it uses the same number named num. If so, you can use:

n = 1000
num = 1 # the number you want to repeat
#Execute for 0.06280231475830078s
for i in [num]*n: 
    print(i)

or

n = 1000
num = 1 # the number you want to repeat
#Execute for 0.05784440040588379s
for _ in range(n):
    print(num)
keineahnung2345
  • 2,635
  • 4
  • 13
  • 28
  • @Sam Is it work for your question: `I understand this, but is there a way for me to get a list of the same numbers from the for loop?` – keineahnung2345 Jan 15 '19 at 09:11