Oct-23-2019, 02:49 AM
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):
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.
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 totalnumberFunction 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.