Home > front end >  why is my if else statement terminating at if even with different values (python)
why is my if else statement terminating at if even with different values (python)

Time:12-22

The program is terminating for the if else ladder on the first step and showing the same result over and over again even with different inputs here is the code

print("let me tell you a bit regarding to your birth month and all that stuff")

date = int(input("tell me your date of birth. Only date!"))

month = input("tell me the month in which you were born. Only month!")

year = int(input("tell me in which year you were born. Only year!"))

new_month = month.lower()

if date < 31 or date < 30 and month == "march" or month == "april" and date <= 30 or date <= 31 and date != 20:
  print(
    "you are Aries. Independent and strong‒willed, you are a force to be reckoned with! You love nothing more than an exciting new goal to tackle, and you do your best work when you’re flying solo. Your passion and energy keep the rest of us on our toes!"
  )

elif date < 31 or date < 30 and month == "april" or month == "may" and date <= 30 or date <= 31:
  print("you are a Taurus. As a Taurus, you’re a wonderful combination of laid‒back and hard‒working. You’re honest and loyal, occasionally to a fault. Your determination and attention to detail will take you far in life. ")

elif date < 31 or date < 30 and month == "may" or month == "june" and date <= 30 or date <= 31:
  print("you are a Gemini. Your ability to get along with a wide variety of people makes you a bit of a social butterfly, but you’ll take advantage of some alone time when it comes your way. Curious and deeply emotional, you love ritual and celebration.")

elif date < 31 or date < 30 and month == "june" or month == "july" and date <= 30 or date <= 31:
  print("you are a Cancer. Your intuition is downright uncanny! You do your best socializing in small groups and prefer intimate relationships even if it means your social circle is on the smaller side. Your creative spirit will bring joy to all you meet.")

elif date < 31 or date < 30 and month == "july" or month == "august" and date <= 30 or date <= 31:
  print("you are a leo. It’s no wonder your symbol is a lion. Your personality and presence are impressive to all. This may intimidate some, but your inviting spirit will help you easily make friends. Your confidence will be an asset to you throughout your life.")

elif date < 31 or date < 30 and month == "august" or month == "september" and date <= 30 or date <= 31:
  print("you are a Virgo. You are the picture of poise and elegance. You love to stay organized and have a strong focus on keeping things aesthetic. But you’re not just beauty. You’ve got brains, too! You’ll continue seeking knowledge and intellectual growth as you age.")

elif date < 31 or date < 30 and month == "september" or month == "october" and date <= 30 or date <= 31:
  print("you are a libra. You have a large social circle, and your open‒mindedness helps you get along with just about anyone. But don’t get lost in the crowd! A focus on self‒care and personal reflection will help you build your confidence over time.")

elif date < 31 or date < 30 and month == "october" or month == "november" and date <= 30 or date <= 31 and date != 22:
  ("you are a Scorpio. As a Scorpio, you can have a sharp edge, but this isn’t always a negative quality. It gives you an appreciation for authenticity and a strong sense of independence. However, you’re not always as tough as you appear. Once you let people into your life, you’re a bit of a softy. ")

elif date < 31 or date < 30 and month == "november" or month == "december" and date <= 30 or date <= 31:
  print("you are a Sagittarius. The road less traveled is your favorite place to be! Your bravery is admirable and will make you a good fit for leadership roles. You also have a bit of an itch in your shoes and will always be ready to take on a new adventure.")

elif date < 31 or date < 30 and month == "december" or month == "january" and date <= 30 or date <= 31 and date != 20 and date != 21:
  print("you are a Capricon. Your perfectionism and high standards, though sometimes an obstacle, can be one of your superpowers when handled wisely. You have a strong sense of self, which enables you to make meaningful connections and lead the way.")

elif date < 31 or date < 30 and month == "january" or month == "february" and date <= 30 or date <= 31 and date != 20 and date != 19:
  print("you are a Aquarius. You may fall on the introvert side of the spectrum, but that doesn’t mean you don’t know how to have fun. You have an enviable combination of intelligence and intuition, and you are able to identify positive opportunities even in dark times.")

elif date < 31 or date < 30 and month == "february" or month == "march" and date <= 30 or date <= 31:
  print("you are a pisces. You wouldn’t hurt a fly! Empathy is your superpower, and you are an asset to any team you join or cause you support. Your gentleness is a virtue. However, be careful to not let your feelings get hurt too easily. Be sure to spend time building your self‒confidence.")

else :
  print("Invalid output")

enter image description here enter image description here

I thought there will be different outputs like:

"you are a Taurus. As a Taurus, you’re a wonderful combination of laid‒back and hard‒working. You’re honest and loyal, occasionally to a fault. Your determination and attention to detail will take you far in life. "

but this is being showed again and again:

you are Aries. Independent and strong‒willed, you are a force to be reckoned with! You love nothing more than an exciting new goal to tackle, and you do your best work when you’re flying solo. Your passion and energy keep the rest of us on our toes!

CodePudding user response:

You need to use parenthesis, because of operator precedence you can't just write some or/and like you have

if ((date < 31 or date < 30) and month == "march") or (month == "april" and (date <= 30 or date <= 31) and date != 20):
    print("you are Aries. ...")

elif ((date < 31 or date < 30) and month == "april") or (month == "may" and (date <= 30 or date <= 31)):
    print("you are a Taurus. ...")

elif ((date < 31 or date < 30) and month == "may") or (month == "june" and (date <= 30 or date <= 31)):
    print("You are a Gemini. ...")

CodePudding user response:

use the parentheses in conditional statements to separat the condition or and and .. evry conditions on apropriate parenthese.

CodePudding user response:

First of all, I assume you are new to programming because this is not good looking code. I believe your output is incorrect because you are missing parentheses. For example your first if:

if date < 31 or date < 30 and month == "march" or month == "april" and date <= 30 or date <= 31 and date != 20:

First it will check if date < 31. That is true, so then it will go into the if statement. You need to put it like this:

if ((date < 31 or date < 30) and month == "march") or (month == "april" and (date <= 30 or date <= 31) and date != 20):

I also do not understand why you are checking for dates, you could just have it like this:

if (month == "march" or month == "april") and date != 20:

Also, why are you checking if date < 31 and date < 30 on every row. If date < 31 then date < 30 is also true. Even the february row has a date < 31 on it. If you want to check for invalid input, then do it like this:

#Check input
if date > 31 or date < 1:
    print("Invalid date")



if (month == "march" or month == "april") and date != 20:

You can then add more checks for whatever things can go wrong FIRST and keep your later if logic clean.

  • Related