I am currently attempting to write my first program/app in Python by creating a tool for my partner to use when we play a card game. I have just started to pick up learning to program seriously, so it maybe a rookie mistake I have missed, but I can't seem to catch "anything else" via input to divert the user a response.
Here is my code:
def counting_up ():
round_total = 0
while True :
global game_level
cards = input("Enter Card: \n")
if cards.upper() == "A" or cards == "2" :
round_total += 20
if cards == "3" :
round_total += 3
if cards == "4":
round_total += 4
if cards == "5" :
round_total += 5
if cards == "6" :
round_total += 6
if cards == "7" :
round_total += 7
if cards == "8" :
round_total += 8
if cards == "9" :
round_total += 9
if cards == "10" or cards.upper() == "J" or cards.upper() == "Q" or cards.upper() == "K" :
round_total += 10
if cards == "0" :
game_level += 1
if cards.upper() == "END" :
game_state = 1
break
else :
print (f"{cards}, is not a valid value, please enter a valid value!")
print ("Your score this round was " + str(round_total))
return round_total
When testing it doesn't seem to go through the prior logic checks before it comes to the conclusion that its an invalid value. NOTE this entire function was working as intended and does if i remove the else: statement at the end. Is there anything in python similar to a case statement in java that would work?
Thanks in advance
With multiple if statements at the same level, python will check each conditional statement. If you want it to ignore subsequent statements when a prior one has been satisfied, use elif:
def counting_up(game_state, game_level, round_total=0):
"""Request and add input card face value to score repeatedly till end."""
while True:
cards = input("Enter Card: \n")
if cards.upper() == "END":
game_state = 1; break
elif cards == "0":
game_level += 1
elif cards.upper() in ("A", "2"):
round_total += 20
elif cards.isnumeric():
round_total += int(cards)
elif cards.upper() in ("J","Q","K"):
round_total += 10
else:
print(f"{cards}, is not a valid value, please enter a valid value!")
print(f"Your score this round was {round_total}.")
return round_total
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With