-2

I need to write a code that returns the longer card notation from the shorter input and completed that part, but I need to also tell it to return "invalid" if the notation entered is not eligible or in the list.

Currently if I put in an else function it just gives me invalid if I input in something with a longer length than asked, but if I typed something within the length limit but still not in the list it just gives me an error.

So how do I tell the program to return "invalid" for any wrong value within or outside the length limit?

Here is the code:

cardValues = {"A": "Ace", "a": "Ace", "J":"Jack", "j": "Jack", "Q": "Queen", "q": "Queen", "K": "King", "k": "King", "2": "Two", "3": "Three", "4":"Four", "5": "Five", "6": "Six", "7": "Seven", "8": "Eight", "9": "Nine", "10":"Ten" }

cardShapes = {"D": "Diamonds", "H": "Hearts", "S": "Spades", "C": "Clubs", "d": "Diamonds", "h": "Hearts", "s": "Spades", "c":"Clubs"}

Notation = input("Enter card notation: ")

if len(Notation) == 2:

    value = Notation[0]
    shape = Notation[1]
    print(cardValues.get(value) + " of " + cardShapes.get(shape))

elif len(Notation) == 3:

    value = Notation[0:2]
    shape = Notation[2]
    print(cardValues.get(value) + " of " + cardShapes.get(shape))
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
sba
  • 1
  • 1
  • 1
    Does this answer your question? [Check if a given key already exists in a dictionary](https://stackoverflow.com/questions/1602934/check-if-a-given-key-already-exists-in-a-dictionary) – mkrieger1 Dec 13 '21 at 15:49
  • The `get` method is used in order to return a default value if a key is not contained in a dictionary. You don't want that, so the `get` method is the wrong way to access the dictionaries in this case. – mkrieger1 Dec 13 '21 at 15:51

1 Answers1

1

you can check if value and shape are in the cardValues and cardShapes dictionaries.

you can do this like so:

if value in cardValues and shape in cardShapes:
    print(cardValues.get(value) + " of " + cardShapes.get(shape))
else:
    print("invalid")
DorElias
  • 2,243
  • 15
  • 18