DartRuffian
DartRuffian

Reputation: 13

An elif statement seems to not be running when it should

The elif statement "elif compScore > 14 and compScore < 18 and choice != "y":" doesn't seem to be running. This is for a dice rolling game for school, the point is to get to 18 without going over. The computer has some rules, like it won't roll if it has a 15 or higher.

from random import randint

playerScore = compScore = 0
choice = "y"

def RollD6():
    return(randint(1, 6))

def CompTurn():
    global compScore
    if compScore <= 14:
        compScore += RollD6()
    print("compScore = '%s'" % compScore)

def PlayerTurn():
    global playerScore
    choice = input("\nYour current score is %s. Would you like to roll a d6? [y/n] " % playerScore).lower()
    print("choice = '%s'" % choice)
    if choice == "y":
        playerScore += RollD6()
    CompTurn()

print("Welcome to a Black Jack-ish Dice Game!\nThe goal is to roll a D6 until either you or the computer gets to exactly 18.\nBut if you roll over 18 you lose.")
while True:
    if compScore == 18:
        print("The computer rolled to 18 faster, better luck next time.")
        break

    elif playerScore == 18:
        print("Your score was %s." % playerScore)
        print("Congrats, you ended up beating the computer by %s points." % str(playerScore - compScore))
        break
# V This is the elif statement, I've tried changing it quite a bit, along with the order V
    elif compScore > 14 and compScore < 18 and choice != "y": 
        print("Your score was %s." % playerScore)
        print("It seems you would've gotten into a loop with the computer, so the computer wins.")
        break

    elif playerScore > 18:
        print("Your score was %s." % playerScore)
        print("Whoops, guess you got a little greedy. The computer won because you rolled over 18.")
        break

    elif compScore > 18:
        print("Your score was %s." % playerScore)
        print("The computer ended up rolling over 18, so you win!")
        break
    PlayerTurn()

Upvotes: 0

Views: 56

Answers (2)

user1558604
user1558604

Reputation: 987

While global will likely make this work, perhaps a more pythonic way would be to have the PlayerTurn function return a value to use. For example:


def PlayerTurn():
   ...
   return choice

...

while True:
   ...
   choice = PlayerTurn()
   CompTurn() # Optional, but I recommend removing this function call  from the PlayerTurn() call. 
   #If someone was to look at your code cold, they may wonder where the CompTurn() gets called, 
   #because it is inconsistent for that to be in the PlayerTurn() function.

Upvotes: 0

Matt Werenski
Matt Werenski

Reputation: 291

The reason that it is never going into that case is that the choice variable is not actually being updated. The choice defined in the outermost scope must be marked as global in the PlayerTurn function just like playerScore.

def PlayerTurn():
    global playerScore
    global choice # <--- You need this line
    choice = input("\nYour current score is %s. Would you like to roll a d6? [y/n] " % playerScore).lower()
    print("choice = '%s'" % choice)
    if choice == "y":
        playerScore += RollD6()

Upvotes: 1

Related Questions