Home > Software engineering >  problem with telling program to find invalid notation in python
problem with telling program to find invalid notation in python

Time:12-14

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))

CodePudding user response:

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")
  • Related