Search code examples
pythondice

Guess the highest number issue


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 def 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:

import random
import time
from random import randint
play_again = True

#greeting the players to the game
print("Welcome to Beat That, a small game made by Mats Muche.")

#as long as play_again is true this will repeat itself until the user decides to end the game
while play_again:
    totalnumber = []

    def rollDice(numOfDice):
        num = []
        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 num
        return totalnumber

    #this part checks the players guess to the highest number that can be made
    def turn(numOfDice):
        roll = rollDice(numOfDice)
        totalnumber.sort(reverse = True)
        print(*totalnumber , sep="")
        guess = int(input("What is the biggest number you can make?"))
        if guess == totalnumber:
            print("Correct!")
        else:
            if totalnumber != guess:
                print("Incorrect!")
                print("The highest number you could've made out of your roll is ", *totalnumber , sep="")
        time.sleep(1)
        return totalnumber

    #main code
    #rules
    print("*" * 80)
    print("Here are the rules!")
    time.sleep(1)
    print("-Players may take turns rolling a set number of dice.")
    time.sleep(1)
    print("-The aim of the game is to get the biggest number from your dice roll.")
    print("*" * 80)
    time.sleep(2)

    #amount of dice players want to use
    numOfDice = int(input("How many dice would you like to use? "))

    #start of game
    print("Player 1's turn:")
    time.sleep(1)
    p1 = turn(numOfDice)
    print("*" * 80)
    time.sleep(2)
    print("Player 2's turn:")
    time.sleep(1)
    totalnumber = []
    p2 = turn(numOfDice)
    print("*" * 80)

    #seeing who won the game (highest number made wins)
    if p1 > p2:
        print("Player 1 has won the game! Congrats!")
        time.sleep(1)
    elif p2 > p1:
        print("Player 2 has won the game! Congrats!")
        time.sleep(1)
    else:
        print("It's a tie! Try again.")
    print("*" * 80)

    #seeing if players want to play again
    again = input("Do you want to play again? Press any key except from 'n' to continue. ")
    if again[0].lower() == 'n':
        play_again = False

#if players say "n" then this message pops up and the game ends
print("End of game. Thank you for playing!")

Thanks for reading :)

As I am a beginner who is in school. I do not really have much knowledge of how to fix something like this issue.

This is the line that is the problem.

print("The highest number you could've made out of your roll is ", *totalnumber , sep="")

Solution

  • The problem would be this line:

    def turn(numOfDice):
            roll = rollDice(numOfDice)
            totalnumber.sort(reverse = True)
            print(*totalnumber , sep="")
            guess = int(input("What is the biggest number you can make?"))
            if guess == totalnumber:
                print("Correct!")
    

    Here, totalnumber is a list, not an int. therefore, you can try to make the input similarly a list too. change:

    guess = int(input("What is the biggest number you can make?"))

    into:

    guess = list(map(int, input("What is the biggest number you can make?")))

    Which should fix the issue.