samedi 11 mars 2023

Python Random Int Generation returning 'None' instead of a integer [closed]

I have a very simple dice rolling program, with the main body of the program here.

def roll():
 rollOne = random.randint(1,6)
 rollTwo = random.randrange(1,6)
 if rollOne == rollTwo:
   score = (rollOne + rollTwo) * 2
 else:
   score = rollOne + rollTwo
   return score


for i in range(rounds):
 playerOneRoll = roll()
 print("Player 1 rolled a",playerOneRoll)
 playerOneScore = int(playerOneScore) + int(playerOneRoll)
 print("Player 1's total score is",playerOneScore)
 print("----------------------------")
 sleep(delay)
 roll()
 playerTwoRoll = roll()
 print("Player 2 rolled a",playerTwoRoll)
 playerTwoScore = int(playerTwoScore) + int(playerTwoRoll)
 print("Player 2's total score is",playerTwoScore)
 print("----------------------------")
 sleep(delay)

It works fine for a couple rounds but then sometimes the player one roll will return as 'None' and the program will end due to an error. Can't figure out why. Attached is an image of the output. output Any comments or ideas about this would help a lot - many thanks.




Aucun commentaire:

Enregistrer un commentaire