#🔒 "Double" not printing when a double is rolled

103 messages · Page 1 of 1 (latest)

mortal sirenBOT
#

@zenith grove

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

rigid summit
#

@zenith grove I've recovered your message

HI, i have made this code (i know it could use a bit of cleaning up but i was just focussed on making it function at the moment) in which the user rolls dice and gets money, but when the user rolls a double, it should print "Double" but it doesn't

#

If you'd like to share your code, you can use a paste link

#

!paste

mortal sirenBOT
#
Pasting large amounts of code

If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/

After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.

zenith grove
rigid summit
#

Ok woah, there's a lot going on here

glass mesa
#

indeed im lost too

zenith grove
#

the main bit im worried about is line 95 - 98

rigid summit
#

This needs a major refactor. I wouldn't even begin trying to debug these conditions

#
if d1 == d2 or d1 == d3 or d1 == d4 or d1 == d5 or d1 == d6 or d2 == d3 or d2 == d4 or d2 == d5 or d2 == d6 or d3 == d4 or d3 == d5 or d3 == d6 or d4 == d5 or d4 == d6 or d5 == d6 and sextuple == False and quintuple == False and quadruple == False and triple == False
glass mesa
#

yeah i dont want to touch those either

zenith grove
#

yeah i know its a bit of a mess, how would i clean that bit up?

rigid summit
#

Using this many variables will lead to issues. You should ideally clean it up to use different data structures like lists or dicts to keep track of the data

glass mesa
#

and probably use a dice module to roll these die

rigid summit
#

random is perfectly fine for this

zenith grove
#

yeah i am using random

glass mesa
#

i only saw dice, i didnt look too much into how hes rolling

rigid summit
#

any time you have repetition in variable name, always consider a different structure

#
d1 = 0
d2 = 101
d3 = 102
d4 = 103
d5 = 104
d6 = 105
#

it's very unclear what these are even meant to represent

glass mesa
#

also, do you mean this?

Die 1: 5
Die 2: 2    ( x2.5 = 5.0 )
DOUBLE!!! ( x5 )

Total: 50.0

Money: £237.5

Next upgrade: £2500
```bc i started playing and it did show
zenith grove
zenith grove
glass mesa
#

ok i see, so its an issue in your big comparison

zenith grove
#

yeah but how would i condense it?

#

and make it work

glass mesa
#

trying to figure that out myself, gotta get my ide to stop throwing style errors first though

#
  if multi2_2:
    m2 = random.choice(multi2)
    print("\nMulti 2: " + str(m2))

you dont need multi2_2 == True, multi_2 is valid bc it is a bool and is no different than if True

#

https://paste.pythondiscord.com/AI4A

here it is with all the reformatting on the ifs and condiditions - ive not made any changes to the code or the if blocks just made them slightly more readable so others can help you more if they see something before i do

zenith grove
glass mesa
#

if so theres an extension that will point out the style errors i fixed. while it wont fix the root problem of the complicated if statements it will at least help you make them more readable

zenith grove
glass mesa
#

now i just finished my lunch so ill take another look at your code. have you made any changes?

glass mesa
#

rn im just remaking it bc it looks like a fun project to test something ive been developing bc i dont know if youve made changes or not

zenith grove
glass mesa
#

also big question, is this homework or a self project?

#

bc i think ive finished my remake

zenith grove
# glass mesa what are the multi's for?

oh i hadn’t got to that bit yet but once the user had unlocked them they would multiply the total outcome of the roll, and they would be upgraded to give a bigger multiplier when the user has enough money

zenith grove
zenith grove
glass mesa
#

cool, bc i didnt remake that part. and im prob going to send it in chunks so you can ask questions as you work it out

#
occurence = max(Counter(dice_rolls).values())
```here is your first hint though as to the huge if statements. if you put your rolls in a list you can count how often they happen.
zenith grove
#

oh okay yeah that’s better

glass mesa
#
import multi_dice
dice_rolls: list = multi_dice.RollDice(f"{self.dice_amount}d6").rolls
```i dont know about other dice modules but this is the one im using. it just generates the list for you which is how i got the list of rolls
zenith grove
#

alright yeah that looks good

glass mesa
#

do you understand how f strings work? bc that chunk i think the f string is weird part for most new programmers

zenith grove
#

not really but i trust you 😂, what is an f string then

glass mesa
#

so you know how you can do "string" + str(variable) + "string"? basically its the same thing but instead its f"string{variable}string" making it easier to read

#

you can also run functions in the {}

zenith grove
#

ohh yeah there’s a similar thing in javascript

glass mesa
#
occurence = max(Counter(dice_rolls).values())
        if occurence >= 2:
            print(f"\033[1;32m{self.text[occurence]}!!! ( {total} x {5**occurence if occurence > 2 else 5} )\033[0m\n")
            total *= 5**occurence if occurence > 2 else 5
```and this is how handled the printing of double and everything making use of an f string
zenith grove
#

oh yeah wow that looks way better than my previous code

glass mesa
#
    multiplier = {
        1:1,
        2:2.5,
        3:7.5,
        4:22.5,
        5:67.5,
        6:202.5
    }

    text = {
        2: "DOUBLE",
        3: "TRIPLE",
        4: "QUADRUPLE",
        5: "QUINTUPLE",
        6: "SEXTUPLE"
    }

    upgrades: list = [75, 2500, 75000, 2500000, 75000000]
```everythng it pulls from is an easy to use list it can just look up
zenith grove
#

yeah that’s a lot better

glass mesa
#

im going to let you refactor some of the code and when you send what you think looks good ill send prob the biggest shock that will make you want to rewrite all of it, the only hint im going to give you is the self.varnames throughout my code

zenith grove
#

alright thanks you so much, i’ll have a look in about 10 mins

zenith grove
glass mesa
#

thats bc you need to install it with pip install multi-dice

#

its not a default module

zenith grove
#

oh im not sure im able to do that then in the browser based ide im using, ill just do it in vs

glass mesa
zenith grove
glass mesa
#

but i would personally recommend moving away from the browser stuff anyway

zenith grove
#

yeah i will do soon

glass mesa
#

but if it works for you it works for you

zenith grove
#

okay so can you explain what the self. does please

glass mesa
#
class Player():
    def __init__(self):
        self.money: int = 0
        self.dice_amount: int = 1

    multiplier = {
        1:1,
        2:2.5,
        3:7.5,
        4:22.5,
        5:67.5,
        6:202.5
    }

    text = {
        2: "DOUBLE",
        3: "TRIPLE",
        4: "QUADRUPLE",
        5: "QUINTUPLE",
        6: "SEXTUPLE"
    }

    upgrades: list = [75, 2500, 75000, 2500000, 75000000]

    def roll(self):
```its a class thing. so everything is contained in the class without the need for globals
zenith grove
#

ohhh so if i put the variables in that class i wouldnt need to use global?

glass mesa
#

exactly, the roll function is also in the class

#

other than imports this is the only thing not in my class in my remake

game = Player()

while True:
    key = input("q to quit, enter to continue:")
    if key.lower() == "q":
        break
    game.roll()
zenith grove
#

alright thanks

glass mesa
zenith grove
#

yeah i have just run into an error

#

line 63, in roll occurence = max(Counter(dice_rolls).values()) TypeError: 'int' object is not callable

glass mesa
#

hrm, can you post the whole code? this looks to be an issue further up in the code

#

something tells me i know what the issue is, but i want to see it all in case its not

zenith grove
glass mesa
#

i found the issue

#

it was an import issue, you assigned Counter to 1 when its actually an import, about to paste the fix. i also fixed the very bottom of your code too bc you had an extra roll()

zenith grove
#

alright thanks

glass mesa
#

im just pointing you in the right direction until you get it working and once you do is when ill share mine

zenith grove
#

yeah i feel this is more useful than just you giving me the answer because i actually understand how its working

glass mesa
#

unless its something simple this is how i normally do things. and making something super complicated to something simple is definetly something that takes a lot of practice

#

do you understand what the var:int = 0 is? a lot of people have trouble with that one too

zenith grove
#

yeah does it not just make it an integer value?

glass mesa
#

it does, its called typehinting, lets me do something like this line dice_rolls: list = multi_dice.RollDice(f"{self.dice_amount}d6").rolls and tells you who doesnt know how this function works that its going to return a list into that value

#

its more to tell people what the variable is rather than the computer

zenith grove
#

ohhh right yeah that makes sense

glass mesa
#

hows it coming along @zenith grove ?

zenith grove
glass mesa
#

ok youve cought it up it seems ill post what i have. i did just make the upgrade auto buy though but probably make a shop function in the class that lets you see the upgrades available

#
import multi_dice
from collections import Counter

class Player():
    def __init__(self):
        self.money: int = 0
        self.dice_amount: int = 1

    multiplier = {
        1:1,
        2:2.5,
        3:7.5,
        4:22.5,
        5:67.5,
        6:202.5
    }

    text = {
        2: "DOUBLE",
        3: "TRIPLE",
        4: "QUADRUPLE",
        5: "QUINTUPLE",
        6: "SEXTUPLE"
    }

    upgrades: list = [75, 2500, 75000, 2500000, 75000000]

    def roll(self):
        dice_rolls: list = multi_dice.RollDice(f"{self.dice_amount}d6").rolls
        current_die: int = 1
        total:int = 0
        for roll in dice_rolls:
            print(f"Die {current_die}: {roll} (*{self.multiplier[current_die]} = {roll*self.multiplier[current_die]})")
            total += roll*self.multiplier[current_die]
            current_die += 1

        occurence = max(Counter(dice_rolls).values())
        if occurence >= 2:
            print(f"\033[1;32m{self.text[occurence]}!!! ( {total} x {5**occurence if occurence > 2 else 5} )\033[0m\n")
            total *= 5**occurence if occurence > 2 else 5

        print(f"Total: ${total}\n")
        self.money += total
        print(f"Balance: ${self.money}\n")
        if self.money >= self.upgrades[0]:
            print("NEW DICE!")
            self.upgrades.pop(0)
            self.dice_amount += 1

game = Player()

while True:
    key = input("q to quit, enter to continue:")
    if key.lower() == "q":
        break
    game.roll()
#

the while loop would ideally have nothing in it other than the switching logic changing the bottom to be

key = input("q to quit, enter to continue:").lower()
    if key == "q":
        break
    elif key == '':
      game.roll()
    elif key == 'shop':
      game.shop()
```etc etc. your main game loop shouldnt contain any actual logic
zenith grove
#

yeah thank you so much for your help, can we continue tomorrow because i have to go now, is it ok if i dm you tomorrow?

glass mesa
#

yeah, i already dm'd you even if you didnt notice XD

mortal sirenBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.