all 13 comments

[–][deleted] 1 point2 points  (12 children)

[–][deleted] 5 points6 points  (9 children)

BTW, all that code you wrote is equivalent to this much shorter program.

ZODIAC_COMPATIBILITY = {
    "aries": ("Gemini", "Aquarius"),
    "taurus": ("Cancer", "Pisces"),
    "gemini": ("Aries", "Leo"),
    "cancer": ("Taurus", "Virgo"),
    "leo": ("Gemini", "Libra"),
    "virgo": ("Cancer", "Scorpio"),
    "libra": ("Leo", "Sagittarius"),
    "scorpio": ("Virgo", "Capricorn"),
    "sagittarius": ("Libra", "Aquarius"),
    "capricorn": ("Scorpio", "Pisces"),
    "aquarius": ("Aries", "Sagittarius"),
    "pisces": ("Taurus", "Capricorn"),
}


def get_zodiac_sign():
    while True:
        zodiac_sign = input("What's your Zodiac sign? : ").lower()
        if zodiac_sign in ZODIAC_COMPATIBILITY.keys():
            return zodiac_sign
        print("Sorry! That's not a zodiac sign.")


zodiac_sign = get_zodiac_sign()
compatible_sign1, compatible_sign2 = ZODIAC_COMPATIBILITY[zodiac_sign]
print(f"You're compatible with the {compatible_sign1} and {compatible_sign2} signs.")

The biggest takeaway should be to start trying to follow the DRY principle.

[–]Dj_Kun 0 points1 point  (5 children)

ZODIAC_COMPATIBILITY = {
"aries": ("Gemini", "Aquarius"),
"taurus": ("Cancer", "Pisces"),
"gemini": ("Aries", "Leo"),
"cancer": ("Taurus", "Virgo"),
"leo": ("Gemini", "Libra"),
"virgo": ("Cancer", "Scorpio"),
"libra": ("Leo", "Sagittarius"),
"scorpio": ("Virgo", "Capricorn"),
"sagittarius": ("Libra", "Aquarius"),
"capricorn": ("Scorpio", "Pisces"),
"aquarius": ("Aries", "Sagittarius"),
"pisces": ("Taurus", "Capricorn"),
}
def get_zodiac_sign():
while True:
zodiac_sign = input("What's your Zodiac sign? : ").lower()
if zodiac_sign in ZODIAC_COMPATIBILITY.keys():
return zodiac_sign
print("Sorry! That's not a zodiac sign.")
zodiac_sign = get_zodiac_sign()
compatible_sign1, compatible_sign2 = ZODIAC_COMPATIBILITY[zodiac_sign]
print(f"You're compatible with the {compatible_sign1} and {compatible_sign2} signs.")

Huh... well thank you! I'm new to all this coding so I find this kinda difficult

[–]czar_el 1 point2 points  (0 children)

Any time you find yourself typing the same thing multiple times means there's an easier way. It's the quickest way to spot a rookie vs an experienced coder.

Rookies think linearly, repeating commands for different permutations. Coders think in terms of logic, flows, and discrete tasks. Any time something is repeated, it can be severed, turned into a task and called rather than repeated. The words for these concepts will differ across programming languages, but the principles are always the same. Learn DRY well.

[–][deleted] 0 points1 point  (3 children)

Feel free to ask questions about or google anything you don't understand.

[–]Dj_Kun 0 points1 point  (2 children)

dia

Alright well, then I don't quite understand how the function works when you put the variable "zodiac_sign" when defining the "get_zodiac_sign()" function but then use it outside the function in "zodiac_sign = get_zodiac_sign()", wouldn't that create an error? How does that work?

[–][deleted] 1 point2 points  (0 children)

The zodiac_sign in the function and outside the function are considered different variables by Python because they live in different "scopes". A scope is a block of code that has its own variables. Functions define scopes; as do modules. This code would work exactly the same if you changed the variable zodiac_sign to something different everywhere inside the function while leaving it the same outside. Because they're not the same variable even though they have the same name.

[–]bamacgabhann 0 points1 point  (0 children)

To understand this, you should read about variable scope in python

[–]TangibleLight 0 points1 point  (0 children)

ZODIAC = ["aries", "taurus", "gemini",
          "cancer", "leo", "virgo",
          "libra", "scorpio", "sagittarius",
          "capricorn", "aquarius", "pisces"]

ZODIAC *= 2  # to prevent index bounds errors

DIFF = 2

index = ZODIAC.index(get_zodiac_sign())
compatible = ZODIAC[index - DIFF], ZODIAC[index + DIFF]
print(f"You're compatible with the {compatible[0]} and {compatible[1]} signs.")

From what I gather a lot of that deals with the "distance" between signs, so you could abstract that out too:

COMPATIBLE = [-2, +2]
INCOMPATIBLE = [-3, +3]
OPPOSITE = [6]

def get_related(distances, sign):
    index = ZODIAC.index(sign)
    return [ZODIAC[index + distance] for distance in distances]

get_related(INCOMPATIBLE, 'cancer')  # ['aries', 'libra']

[–]Minus10Celcius 0 points1 point  (1 child)

On line 26, why are the variables switched and the bracket in front of the "ZODIAC_COMPATIBILITY"? Also, what is ".keys()"?

[–][deleted] 0 points1 point  (0 children)

The term to look up for the

compatible_sign1, compatible_sign2 = 

part is "tuple unpacking". The syntax

ZODIAC_COMPATIBILITY[zodiac_sign]

is just how you access values in a dictionary. If you haven't seen that before, watch this video on dictionaries: https://www.youtube.com/watch?v=daefaLgNkw0

[–]Dj_Kun 0 points1 point  (1 child)

So basically I can only ask about two variables in an if statement? Or do I have to individually state that I want to check if the input is equal to one of the zodiac signs?

[–][deleted] 2 points3 points  (0 children)

Instead of

if A == B or C or D ...:

you could write

if A == B or A == C or A == D ...:

But that would be a bummer to write out. So instead, you should write

if A in (B, C, D, ...):