0

After entering the the first two inputs inside the while loop, it fails to proceed to the next IF statement which includes the comparable operator of the previous input. Instead I'm getting the reoccurring inputs. What is the fix?

Thanks.

P.S. I'm a newbie here so apologies for the lack of understanding.

import random

game_started = True


while True:
    name = input("What is your name? ")
    roll_dice = input(f"Would you like to roll the dice {name}?(Y/N) ").lower
    
    if roll_dice == "y":
        player_number = random.randint(1, 6)
        print(f"You rolled a {player_number}!")
        ai_number = random.randint(1, 6)
        print(f"The computer rolled the die and got {ai_number}")
        
        if player_number > ai_number:
            print(f"YOU WIN! Your die number of {player_number} was bigger than the computers rolled die of {ai_number}")
            break
        elif player_number < ai_number:
            print(f"YOU LOST! The computer rolled a higher number of {ai_number} to your {player_number}")
            break
        elif player_number == ai_number:
            print(f"IT'S A DRAW! Both the you and the computer rolled the same number of {player_number}")
            break
wjandrea
  • 28,235
  • 9
  • 60
  • 81
Fman_95
  • 9
  • 3
  • Welcome to SO! Check out the [tour]. For debugging help, you need to provide a [mre] as text in the question itself. You can [edit] the question. Check out [ask] if you want more tips. – wjandrea Nov 25 '20 at 23:11
  • 3
    You wrote `.lower` instead of `.lower()`. Seems to be a typo. – wjandrea Nov 25 '20 at 23:13
  • If you're confused, check out [Purpose of calling function without brackets python](https://stackoverflow.com/q/21785933/4518341) – wjandrea Nov 25 '20 at 23:21

1 Answers1

2

The while loop is infinite so it need an explicit break to exit:

import random

while True:
    name = input("What is your name? ")
    roll_dice = input(f"Would you like to roll the dice {name}?(Y/N) ").lower()
    
    if roll_dice == "y":
        player_number = random.randint(1, 6)
        print(f"You rolled a {player_number}!")
        ai_number = random.randint(1, 6)
        print(f"The computer rolled the die and got {ai_number}")
        
        if player_number > ai_number:
            print(f"YOU WIN! Your die number of {player_number} was bigger than the computers rolled die of {ai_number}")
            break
        elif player_number < ai_number:
            print(f"YOU LOST! The computer rolled a higher number of {ai_number} to your {player_number}")
            break
        elif player_number == ai_number:
            print(f"IT'S A DRAW! Both the you and the computer rolled the same number of {player_number}")
            break
    else: # When you don't select Y for the roll dice
        break
Exelian
  • 5,749
  • 1
  • 30
  • 49
  • Note that this will continue to ask for your name, which might not be intended. If you don't want this then put the name part outside the while loop – Exelian Nov 25 '20 at 23:20
  • 1
    Also note that you fixed the typo in the question, `.lower` instead of `.lower()` – wjandrea Nov 26 '20 at 00:53