Coming soon - Get a detailed view of why an account is flagged as spam!
view details
1
Using if statements instead of elif statements
Post Body

Forgive me if any of my vocabulary is wrong, I am just starting to learn python.

I made a file for an assignment regarding cards. I think that it creates a class object? with its initiator method? I also included a specific method because we were supposed to tailor it to a file provided to us that called the method.

class Card:
    def __init__(self,suit,number):
        self.suit = suit
        self.number = number

    def get_display_string(self):
        if (self.suit == 1):
            self.suit = "Hearts"
        if (self.suit == 2):
            self.suit = "Diamonds"
        if (self.suit == 3):
            self.suit = "Clubs"
        if (self.suit == 4):
            self.suit = "Spades"
        if (self.number == 1):
            self.number = "Ace"
        if (self.number == 11):
            self.number = "Jack"
        if (self.number == 12):
            self.number = "Queen"
        if (self.number == 13):
            self.number = "King"
        return "{} of {}".format(self.number, self.suit)

I ran it a bunch and to my knowledge there was no mistakes in regards to getting the desired output. I got full credit as well, but the teacher comments stated I should be using elif instead of just if for comparing items in the same sequence. Is there a reason for this? What problems could arise? Did I just get lucky that this somehow worked?

Author
Account Strength
90%
Account Age
9 years
Verified Email
Yes
Verified Flair
No
Total Karma
2,919
Link Karma
439
Comment Karma
2,367
Profile updated: 1 month ago
Posts updated: 1 month ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
3 years ago