Python Forum

Full Version: Pyhton Coding Help Needed (very small error)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello, I am creating a Python Dice game called Beat That. I have gotten everything to work so far except for taking the players guess and comparing it to the highest possible number you can make. So let's say you roll a 5 and a 2 the highest number you could make it 52. So far when I enter the correct number it always says incorrect. Any help is appreciated.

In the screenshot below everything works except for the define turn section where it says "The highest number you could've made out of your roll is ...". It prints out the correct number but it marks it as incomplete.

This is the whole code: https://pastebin.com/2zaGZC9D

Thanks for reading :)
There are a few issues with your code.

The main problem you're encountering seems to be a data type issue. Comparing guess (data type: int) to totalnumber (data type: list) will fail because those types cannot be compared. With your current implementation, you would need to join the contents of totalnumber and then convert to an int.

In rollDice() and turn(), totalnumber is referenced, but it's a global variable. Don't use global variables - they aren't worth it. Instead, the variable should be returned (some additional edits included):

    def rollDice(numOfDice):
        totalnumber = []
        for i in range(1,numOfDice+1):
            num = randint(1, 6)
            print("Rolling the dice... You rolled a",num)
            totalnumber.append(num)
            time.sleep(1.5)
        return map(str, totalnumber) # Returns totalnumber after converting items to strings.
 
    #this part checks the players guess to the highest number that can be made
    def turn(numOfDice):
        totalnumber = rollDice(numOfDice)
        totalnumber.sort(reverse = True)
        totalnumber = int("".join(totalnumber)) # Joins the list items into a single value and converts to int
        print(totalnumber , sep="")
        guess = int(input("What is the biggest number you can make?"))
        if guess == totalnumber:
            print("Correct!")
        else:
            print("Incorrect!")
            print("The highest number you could've made out of your roll is ", *totalnumber , sep="")
        time.sleep(1)
        return totalnumber
Function and class definitions should not be in the while loop. This will slow down your program.

Line 14 doesn't make sense in the surrounding function. The variable num is created as a list but it's changed to an int on line 16.

Line 21 doesn't do anything. Because there's a return statement on line 20, rollDice() terminates on line 20.

Line 32 is unnecessary. The conditional on line 29 checks for equality between guess and totalnumber; if they are not equal, the else body is run. So, checking inequality in the else body is not necessary.
In order to prepare further refactoring and extensions, I would structure the code with a class
class Game:
    def roll_dice(self, num_of_dice):
        ...

    def turn(self, num_of_dice):
        ...

    def play_forever(self):
        self.total_number = []
        self.play_again = True
        while self.play_again:
            self.play_once()
        print("End of game. Thank you for playing!")

    def play_once(self):
        # main code goes here
        ...

if __name__ == '__main__':
    Game().play_forever()