Python Forum
Pyhton Coding Help Needed (very small error)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pyhton Coding Help Needed (very small error)
#1
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 :)
Reply
#2
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.
Reply
#3
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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Coding error. xflyerwdavis 2 486 Oct-07-2023, 07:08 PM
Last Post: deanhystad
  Vlookup function in Pyhton antozas 1 590 Oct-02-2023, 04:16 AM
Last Post: vikraman
  Coding error. Can't open directory EddieG 6 1,063 Jul-13-2023, 06:47 PM
Last Post: deanhystad
  Coding Error EddieG 2 509 Jul-09-2023, 02:59 AM
Last Post: EddieG
  Help needed with a "for loop" + error handling tamiri 2 2,390 May-27-2022, 12:21 PM
Last Post: tamiri
  Reading Multiple text Files in pyhton Fatim 1 1,886 Jun-25-2021, 01:37 PM
Last Post: deanhystad
  python coding error isntitzee 1 2,179 Oct-17-2020, 06:30 AM
Last Post: buran
  Coding error- Not sure where I have put error markers against the code that is wrong Username9 1 1,692 Sep-28-2020, 07:57 AM
Last Post: buran
  coding error iro a menu Rollo 2 2,037 Sep-27-2020, 04:17 PM
Last Post: deanhystad
  Adafruit LIS3DH coding error FitDad73 1 1,970 Aug-30-2020, 01:37 AM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020