Zodiacal sign python - python

import os
from datetime import date
def program():
Year = input("year of birth:" )
Month = input("month of birth:" )
Day = input("day of birth:" )
Date_of_Birth = (Day + "/" + Month + "/" + Year)
print('Your Date of Birth is ' + Date_of_Birth)
d = date.today()
y = d.year
os.system("cls")
age = y - int(Year)
print('Your age is ' + str(age))
def zodiac_sign():
if (int(Month)==12<2):
print("\n Capricorn")
elif (int(Month)==1<3):
print("\n aquarium")
elif (int(Month)==2<4):
print("\n Pices")
elif(int(Month)==3<5):
print ("\n Aries")
elif(int(Month)==4<6):
print("\n Taurus")
elif(int(Month)==5<7):
print("\n Gemini")
elif(int(Month)==6<8):
print("\n cancer")
elif(int(Month)==7<9):
print ("\n leo")
elif(int(Month)==8<9):
print ("\n virgo")
elif(int(Month)==9<10):
print ("\n libra")
elif(int(Month)==10<12):
print ("\n Scorpio")
elif(int(Month)==11<13):
print("\n Sagittarius")
zodiac_sign()
input()
program()
I'm trying to get the zodiac sign but I can't find a way to insert the number. I already tried to put it like this:
if (int(Month)==12<2 , int(day)==22<1):
print("\n Capricorn")
etc etc
but it keeps saying "capricorn" no matter which date I put in. Can you give me any solution?
I'm using python V3.4.0

import os
from datetime import date
def program():
Year = input("year of birth:" )
Month = input("month of birth:" )
Day = input("day of birth:" )
Date_of_Birth = (Day + "/" + Month + "/" + Year)
print('Your Date of Birth is ' + Date_of_Birth)
d = date.today()
y = d.year
os.system("cls")
age = y - int(Year)
print('Your age is ' + str(age))
if ((int(Month)==12 and int(Day) >= 22)or(int(Month)==1 and int(Day)<= 19)):
Signo_Zodiacal = ("\n Capricorn")
elif ((int(Month)==1 and int(Day) >= 20)or(int(Month)==2 and int(Day)<= 17)):
zodiac_sign = ("\n aquarium")
elif ((int(Month)==2 and int(Day) >= 18)or(int(Month)==3 and int(Day)<= 19)):
zodiac_sign = ("\n Pices")
elif ((int(Month)==3 and int(Day) >= 20)or(int(Month)==4 and int(Day)<= 19)):
zodiac_sign = ("\n Aries")
elif ((int(Month)==4 and int(Day) >= 20)or(int(Month)==5 and int(Day)<= 20)):
zodiac_sign = ("\n Taurus")
elif ((int(Month)==5 and int(Day) >= 21)or(int(Month)==6 and int(Day)<= 20)):
zodiac_sign = ("\n Gemini")
elif ((int(Month)==6 and int(Day) >= 21)or(int(Month)==7 and int(Day)<= 22)):
zodiac_sign = ("\n Cancer")
elif ((int(Month)==7 and int(Day) >= 23)or(int(Month)==8 and int(Day)<= 22)):
zodiac_sign = ("\n Leo")
elif ((int(Month)==8 and int(Day) >= 23)or(int(Month)==9 and int(Day)<= 22)):
Signo_Zodiacal = ("\n Virgo")
elif ((int(Month)==9 and int(Day) >= 23)or(int(Month)==10 and int(Day)<= 22)):
zodiac_sign = ("\n Libra")
elif ((int(Month)==10 and int(Day) >= 23)or(int(Month)==11 and int(Day)<= 21)):
zodiac_sign = ("\n Scorpio")
elif ((int(Month)==11 and int(Day) >= 22)or(int(Month)==12 and int(Day)<= 21)):
zodiac_sign = ("\n Sagittarius")
print(zodiac_sign)
program()
fixed by me

def again():
print("")
print("Please check you month")
print("Month must be the following")
print("01 - January")
print("02 - Febuary")
print("03 - March")
print("04 - April")
print("05 - May")
print("06 - Jun")
print("07 - Jully")
print("08 - August")
print("09 - September")
print("10 - October")
print("11 - Novemver")
print("12 - December")
print("")
def zodiac():
month = input("Month of birth(eg. 01,04,,12):")
day = int(input("Day of birth:"))
bday = ( month + "/" + str(day))
if (int(month) > 12):
again()
zodiac()
elif month == "01":
if (day > 31):
print("")
print("Invalid Date")
print("Please try again!")
print("")
zodiac()
elif (day <= 20):
print("Your Birthday is(mm/dd):",bday)
print("Then your zodiac sign is Capricorn!")
else:
print("Your Birthday is(mm/dd):",bday)
print("Then your zodiac sign is Aquarius")
elif month == "02":
if (day >28):
print("")
print("Invalid Date")
print("Please try again")
print("")
zodiac()
elif (day <= 18):
print("Your Birthday is(mm/dd):",bday)
print("Then your zodiac sign is Aquarius")
else:
print("Your Birthday is(mm/dd):",bday)
print("Then your zodiac sign is Pisces")
elif month == "03":
if (day > 31):
print("")
print("Invalid Date")
print("Please try again!")
print("")
zodiac()
elif (day <= 20):
print("Your Birthday is(mm/dd):",bday)
print("Then your zodiac sign is Pisces")
else:
print("Your Birthday is(mm/dd):",bday)
print("Then your zodiac sign is Aries")
elif month == "04":
if (day > 30):
print("")
print("Invalid Date")
print("Please try again!")
print("")
zodiac()
elif (day <= 20):
print("Your Birthday is(mm/dd):",bday)
print("Then your zodiac sign is Aries")
else:
print("Your Birthday is(mm/dd):",bday)
print("Then your zodiac sign is Taurus")
elif month == "05":
if (day > 31):
print("")
print("Invalid Date")
print("Please try again!")
print("")
zodiac()
elif (day <= 20):
print("Your Birthday is(mm/dd):",bday)
print("Then your zodiac sign is Taurus")
else:
print("Your Birthday is(mm/dd):",bday)
print("Then your zodiac sign is Gemini")
elif month == "06":
if (day > 30):
print("")
print("Invalid Date")
print("Please try again!")
print("")
zodiac()
elif (day <= 21):
print("Your Birthday is(mm/dd):",bday)
print("Then your zodiac sign is Gemini")
else:
print("Your Birthday is(mm/dd):",bday)
print("Then your zodiac sign is Cancer")
elif month == "07":
if (day > 31):
print("")
print("Invalid Date")
print("Please try again!")
print("")
zodiac()
elif (day <= 22):
print("Your Birthday is(mm/dd):",bday)
print("Then your zodiac sign is Cancer")
else:
print("Your Birthday is(mm/dd):",bday)
print("Then your zodiac sign is Leo")
elif month == "08":
if (day > 31):
print("")
print("Invalid Date")
print("Please try again!")
print("")
zodiac()
elif (day <=23):
print("Your Birthday is(mm/dd):",bday)
print("Then your zodiac sign is Leo")
else:
print("Your Birthday is(mm/dd):",bday)
print("Then your zodiac sign is Virgo")
elif month == "09":
if (day > 30):
print("")
print("Invalid Date")
print("Please try again!")
print("")
zodiac()
elif (day <= 23):
print("Your Birthday is(mm/dd):",bday)
print("Then your zodiac sign is Virgo")
else:
print("Your Birthday is(mm/dd):",bday)
print("Then your zodiac sign is Libra")
elif month == "10":
if (day > 31):
print("")
print("Invalid Date")
print("Please try again!")
print("")
zodiac()
elif (day <= 23):
print("Your Birthday is(mm/dd):",bday)
print("Then your zodiac sign is Libra")
else:
print("Your Birthday is(mm/dd):",bday)
print("Then your zodiac sign is Scorpio")
elif month == "11":
if (day > 30):
print("")
print("Invalid Date")
print("Please try again!")
print("")
zodiac()
elif (day <= 23):
print("Your Birthday is(mm/dd):",bday)
print("Then your zodiac sign is Scorpio")
else:
print("Your Birthday is(mm/dd):",bday)
print("Then your zodiac sign is Sagittarius")
elif month == "12":
if (day > 31):
print("")
print("Invalid Date")
print("Please try again!")
print("")
zodiac()
elif (day <= 21):
print("Your Birthday is(mm/dd):",bday)
print("Then your zodiac sign is Sagittarius")
else:
print("Your Birthday is(mm/dd):",bday)
print("Then your zodiac sign is Capricorn")
else:
print("")
again()
zodiac()
zodiac()
print("")
again = input("Type 'zodiac' if you want to try again: ")
if (again == "zodiac"):
zodiac()

Related

Lab #3 combining python codes after importing files where do I start?

Need help figuring out where to start after I import my two previous question files. Everything I've tried doesn't seem to want to take. So I scrapped it and trying to figure out where to begin again
1. In a file called FirstName_LastName_Main.py import Question_1.py and Question_2.py modules.
2. Prompt and read in the month followed by the day from the user. Note: month is of type String and day is of type int.
3. Call Seasons function from Question_1 module with the user’s input as arguments and print the season returned by the call to the function.
4. Prompt and read the total change amount from the user. Note: the total change is an integer.
5. Call the Exact_Change function from Question_2 module with the user’s input as it’s argument.
Sample Output:
Ex. If the input is:
Enter the month: April
Enter the day: 30
Output:
April 30: Spring
Ex. If the input is:
Enter the exact change in cents: 123
Output:
1 Dollar
2 Dimes
3 Pennies
question_1 file code I have -
month = input()
day = int(input())
if month in ('December', 'January', 'February'):
season = 'winter'
elif month in ('April', 'March ', 'May'):
season = 'spring'
elif month in ('June', 'July', 'August'):
season = 'summer'
else:
season = 'fall'
if (month == 'March') and (day >= 20) or (month == 'April') or (month == 'June') and (day >= 20):
season = 'spring'
elif (month == 'June') and (day <= 21) or (month == 'July') or (month == 'August') or (month == 'September') and (day >= 21):
season = 'summer'
elif (month == 'September') and (day >= 22) or (month == 'October') or (month == 'November') or (month == 'December') and (day >= 20):
season = 'fall'
elif (month == 'December') and (day <= 21) or (month == 'January') or (month == 'February') or (month == 'March') and (day >= 19):
season = 'winter'
print(season)
Question_2 file code I have -
amount = input()
if amount <= 0:
print(" No Change ")
else:
dollar = int(amount / 100)
amount = amount % 100
quarter = int(amount / 25)
amount = amount % 25
dime = int(amount / 10)
amount = amount % 10
nickel = int(amount / 5)
penny = amount % 5
if dollar >= 1:
if dollar == 1:
print(str(dollar)+" Dollar")
else:
print(str(dollar)+" Dollars")
if quarter >= 1:
if quarter == 1:
print(str(quarter)+" Quarter")
else:
print(str(quarter)+" Quarters")
if dime >= 1:
if dime == 1:
print(str(dime)+" Dime")
else:
print(str(dime)+" Dimes")
if penny >= 1:
if penny == 1:
print(str(penny)+" Penny")
else:
print(str(penny)+" Pennies")
if nickel >= 1:
if nickel == 1:
print(str(nickel)+" Nickel")
else:
print(str(nickel)+" Nickels")
#SSince it decided to show weirdly above sharing the files below. This is question_2
...
amount = input()
if amount <= 0:
print(" No Change ")
else:
dollar = int(amount / 100)
amount = amount % 100
quarter = int(amount / 25)
amount = amount % 25
dime = int(amount / 10)
amount = amount % 10
nickel = int(amount / 5)
penny = amount % 5
if dollar >= 1:
if dollar == 1:
print(str(dollar)+" Dollar")
else:
print(str(dollar)+" Dollars")
if quarter >= 1:
if quarter == 1:
print(str(quarter)+" Quarter")
else:
print(str(quarter)+" Quarters")
if dime >= 1:
if dime == 1:
print(str(dime)+" Dime")
else:
print(str(dime)+" Dimes")
if penny >= 1:
if penny == 1:
print(str(penny)+" Penny")
else:
print(str(penny)+" Pennies")
if nickel >= 1:
if nickel == 1:
print(str(nickel)+" Nickel")
else:
print(str(nickel)+" Nickels")

Finding Minutes Between Years

can you please check my code? I think something is wrong somewhere.
import datetime
this_year = datetime.datetime.today().year
def age_in_minutes(b_year: int):
d1 = datetime.datetime.strptime(f'{this_year}', '%Y')
d2 = datetime.datetime.strptime(f'{b_year}', '%Y')
age = (d1-d2).days * 24 * 60
return f"{age:,}"
while True:
birth_year = input("Enter you birth year: ")
try:
if int(birth_year) == this_year:
print("Please enter year less than this year...")
elif int(birth_year) > this_year:
print("Please enter year less than this year...")
elif len(birth_year) < 4 or len(birth_year) > 5:
print("PLease enter a valid year...")
elif int(birth_year) <= 1900:
print("Please enter a year after 1900...")
else:
minutes_old = age_in_minutes(int(birth_year))
print(f"You are {minutes_old} minutes old.")
break
except ValueError:
print("Please enter in a year format: ")
I'm tackling a challenge. It said if I enter 1930, I should get "48,355,200". But I'm getting "48,388,320". I'm new to Python.
You could try this code
I found it somewhere in 2021 but I don't know the source
current_year = 2022
year = int(input("Please enter a year: "))
if year == current_year:
print("There are 0 minutes from", year, "to present.")
elif year < current_year:
print("There are", (current_year - year) * 525600, "minutes from", year, "to present.")
else:
print("There are", (year - current_year) * 525600, "minutes from present to", year)
try this:
import datetime
this_year = datetime.datetime.today().year
print(this_year)
def age_in_minutes(b_year: int):
d1 = datetime.datetime.strptime(f'{this_year}', '%Y')
d2 = datetime.datetime.strptime(f'{b_year}', '%Y')
age = (d1-d2).days * 24 * 60
return f"{age:,}"
while True:
birth_year = int(input("Enter you birth year: "))
try:
if birth_year == this_year:
print("Please enter year less than this year...")
elif birth_year > this_year:
print("Please enter year less than this year...")
elif len(str(birth_year)) < 4 or len(str(birth_year)) > 5:
print("PLease enter a valid year...")
elif birth_year <= 1900:
print("Please enter a year after 1900...")
else:
minutes_old = age_in_minutes(birth_year)
print(f"You are {minutes_old} minutes old.")
break
except ValueError:
print("Please enter in a year format: ")
Output:
Enter you birth year: 1930
You are 48,388,320 minutes old.
I don't do any exceptional but change your type casting, and it can give me a desire result.
You haven't calculated leap year. You can follow this:
import datetime
this_year = datetime.datetime.today().year
def age_in_minutes(b_year: int):
leap_year = 0
non_leap_year = 0
for year in range(b_year, this_year+1):
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
leap_year += 1
else:
non_leap_year += 1
leap_year_in_days = leap_year * 366
non_leap_year_in_days = non_leap_year * 365
age = (leap_year_in_days + non_leap_year_in_days) * 24 * 60
return f"{age:,}"
while True:
birth_year = input("Enter you birth year: ")
try:
if int(birth_year) == this_year:
print("Please enter year less than this year...")
elif int(birth_year) > this_year:
print("Please enter year less than this year...")
elif len(birth_year) < 4 or len(birth_year) > 5:
print("PLease enter a valid year...")
elif int(birth_year) <= 1900:
print("Please enter a year after 1900...")
else:
minutes_old = age_in_minutes(int(birth_year))
print(f"You are {minutes_old} minutes old.")
break
except ValueError:
print("Please enter in a year format: ")

How can I short this code with class and object

I am making an investment game in which the Gold and Bitcoin prices go down and up day by day but here I am getting a problem how can I make this code easy so I can add any other crypto and share with just 2-3 lines of code but now I have to code whole if-else statement to do this
Can I use Classes and objects but how
This is the code How can I Improve
goldPrice = 63 # $ dollars per gram
bitcoinPrice = 34000 # $ dollars per bitcoin
day = 1 # life start with this day
myGold = 0 # in Grams
myBitcoin = 0.0 # in bitcoin
def newspaper():
print("GOLD: " + str(goldPrice) + '$ per gram')
print("BITCOIN: " + str(bitcoinPrice) + "$")
print("An investment in knowledge pays the best interest.")
def invest():
global balance, myGold, myBitcoin
print("====Which Thing you want to Buy====")
print("1. Gold")
print("2. Bitcoin")
print("3. Nothing, Go Back")
investInputKey = int(input())
if investInputKey == 1:
print("Gold: " + str(goldPrice) + ' per gram' +'\n')
print("1. Buy")
print("2. Sell")
print("3. Exit")
goldInput = int(input())
if goldInput == 1:
print("Enter Amount in Grams: ")
howMuch = int(input())
totalGoldBuy = howMuch * goldPrice
print(totalGoldBuy)
if totalGoldBuy > balance:
print("Insufficient Balance!")
else:
print("Thank you! Have a Nice Day")
balance -= totalGoldBuy
myGold += howMuch
elif goldInput == 2:
print("Enter Amount in Grams: ")
howMuch = int(input())
if howMuch > myGold:
print("Insufficient Gold!")
else:
print("Thank you! Have a Nice Day")
myGold -= howMuch
newGold = howMuch * goldPrice
balance += newGold
elif goldInput == 3:
print("Thank you! Have a Nice Day")
else:
print("Wrong Input!")
elif investInputKey == 2:
print("Bitcoin: " + str(bitcoinPrice) +'\n')
print("1. Buy")
print("2. Sell")
print("3. Exit")
bitcoinInput = int(input())
if bitcoinInput == 1:
print("Enter Bitcoin Amount in Dollars")
howMuch = int(input())
if howMuch > balance:
print("Insufficient Balance!")
elif howMuch > 500:
myBitcoin = howMuch / bitcoinPrice
balance -= howMuch
else:
print("You can only buy above 500$")
elif bitcoinInput == 2:
print("Enter Bitcoin Amount in Dollars")
howMuch = int(input())
if howMuch > (myBitcoin * bitcoinPrice):
print("Insufficient Bitcoin");
elif howMuch < 500:
print("You can only sale above 500 Dollars")
else:
myBitcoin -= howMuch / bitcoinPrice
balance += howMuch
elif goldInput == 3:
print("Thank you! Have a Nice Day")
else:
print("Wrong Input!")
elif investInputKey == 3:
print("Thank you! Have a Nice Day");
else:
print("Wrong Input!")
while True:
print("========HOME========" + '\n')
print("====Day-" + str(day) + "====")
print("====Balance-" + str(balance) + "$ ====")
print('\n')
print("1. Newspaper")
print("2. Invest")
print("3. My Portfolio")
print("4. Next Day")
print("5. Exit")
inputKey = int(input())
if inputKey == 1:
newspaper()
elif inputKey == 2:
invest()
elif inputKey == 3:
print("========Portfolio========" + '\n')
print("Gold: " + str(myGold) + " gram")
print("Bitcoin: " + str(myBitcoin))
elif inputKey == 4:
day += 1
print("This is day " + str(day))
# Change price of gold and bitcoin
elif inputKey == 5:
# Add sure you want to exit
break
else:
print("Wrong Input! Try Again")
Help me,
Thank You

Zodiac Calculator 2.7.6 Will not calculate sign, line error date not defined. Need help, noob at programming

Year = input("year of birth:" )
Month = input("month of birth:" )
Day = input("day of birth:" )
Date_of_Birth = (str(Day) + "/" + str(Month) + "/" + str(Year))
print('Your Date of Birth is ' + Date_of_Birth)
d = date.today()
y = d.year
age = y - int(Year)
print('Your age is ' + str(age))
if ((int(Month)==12 and int(Day) >= 22)or(int(Month)==1 and int(Day)<= 19)):
Signo_Zodiacal = ("\n Capricorn")
elif ((int(Month)==1 and int(Day) >= 20)or(int(Month)==2 and int(Day)<= 17)):
zodiac_sign = ("\n aquarium")
elif ((int(Month)==2 and int(Day) >= 18)or(int(Month)==3 and int(Day)<= 19)):
zodiac_sign = ("\n Pices")
elif ((int(Month)==3 and int(Day) >= 20)or(int(Month)==4 and int(Day)<= 19)):
zodiac_sign = ("\n Aries")
elif ((int(Month)==4 and int(Day) >= 20)or(int(Month)==5 and int(Day)<= 20)):
zodiac_sign = ("\n Taurus")
elif ((int(Month)==5 and int(Day) >= 21)or(int(Month)==6 and int(Day)<= 20)):
zodiac_sign = ("\n Gemini")
elif ((int(Month)==6 and int(Day) >= 21)or(int(Month)==7 and int(Day)<= 22)):
zodiac_sign = ("\n Cancer")
elif ((int(Month)==7 and int(Day) >= 23)or(int(Month)==8 and int(Day)<= 22)):
zodiac_sign = ("\n Leo")
elif ((int(Month)==8 and int(Day) >= 23)or(int(Month)==9 and int(Day)<= 22)):
Signo_Zodiacal = ("\n Virgo")
elif ((int(Month)==9 and int(Day) >= 23)or(int(Month)==10 and int(Day)<= 22)):
zodiac_sign = ("\n Libra")
elif ((int(Month)==10 and int(Day) >= 23)or(int(Month)==11 and int(Day)<= 21)):
zodiac_sign = ("\n Scorpio")
elif ((int(Month)==11 and int(Day) >= 22)or(int(Month)==12 and int(Day)<= 21)):
zodiac_sign = ("\n Sagittarius")
print(zodiac_sign)
I need some assistance with getting the code to bring up zodiac sign.I'm a noob to coding.Keep getting this error.
Traceback (most recent call last):
File "G:\Python\updatedZodiacSign.py", line 6, in
d = date.today()
NameError: name 'date' is not defined
I need some assistance with getting the code to bring up zodiac sign.I'm a noob to coding.
Put this at the top
from datetime import date
Python doesn't know what a date object is until you import it from the datetime module.

Calendar program help (string variables)

I recently signed up on this site because I am really stuck on the assignment in which I am required to create a program where the user inputs a month and the program displays how many days there are in that month. The user must also input the year so the program can check if it is a leap year in case the user inputs the month of february.
This is the coding I've come up with so far:
def get_year():
year = raw_input("Please enter the year: ")
return year
def get_month():
month = raw_input("Please enter the month: ")
return month
def leap_year(year):
if year % 4 == 0:
return true
else:
return false
def get_days(month):
if month == "january":
print "31 days"
elif month == "february" and leap_year == true:
print "29 days"
else:
print "28 days"
if month == "march":
print "31 days"
elif month == "april":
print "30 days"
elif month == "may":
print "31 days"
elif month == "june":
print "30 days"
elif month == "july":
print "31 days"
elif month == "august":
print "31 days"
elif month == "september":
print "30 days"
elif month == "october":
print "31 days"
elif month == "november":
print "30 days"
elif month == "december":
print "31 days"
else:
print
def main():
user_year = get_year()
user_month = get_month()
leap_year(user_year)
get_days(user_month)
main()
Anyways it's clear that there is an error in my get_days function its just I'm not sure
how to write the code so that the program knows that the user inputs a month such as
january or march. I'm guessing that the input has to be a variable and that since each
month is a string, a variable string is needed. But I could be completely wrong about this.
I'm very new to python (exactly 2 weeks now of off and on programming for school work) so I'm not too sure on many of the specifics of python programming so if anybody could assist me in the proper direction, it would be much appreciated!
You were kinda close. I made some changes which I commented. And as Raymond Hettinger points out, your get_days(month) is completely broken.
def get_year():
year = raw_input("Please enter the year: ")
return int(year) #otherwise it is a string!
def get_month():
month = raw_input("Please enter the month: ")
return month
def leap_year(year):
if year % 4 == 0:
return True
else:
return False
def get_days(month,leap_year): #leap_year must be passes to this function
#This checks for "january" and "february" with leap years
#and falls back to last option on EVERYTHING ELSE like a feb without a leap year or even a "march"
if month == "january":
print "31 days"
elif month == "february" and leap_year == True:
print "29 days"
else:
print "28 days"
#this is a separate block that runs AFTER the previous block
if month == "march":
print "31 days"
elif month == "april":
print "30 days"
elif month == "may":
print "31 days"
elif month == "june":
print "30 days"
elif month == "july":
print "31 days"
elif month == "august":
print "31 days"
elif month == "september":
print "30 days"
elif month == "october":
print "31 days"
elif month == "november":
print "30 days"
elif month == "december":
print "31 days"
else:
print "invalid input" #so that it doesnt fail silently when I enter 2
def main():
user_year = get_year()
user_month = get_month()
leap_status = leap_year(user_year) #store the leap_year status to a variable
get_days(user_month, leap_status) #and then pass it to a function
main()
The if-elif was being broken in February. The minor change is to continue with an uninterrupted pattern of if-elif logic:
def get_days(month):
if month == "january":
print "31 days"
elif month == "february" and leap_year:
print "29 days"
elif month == "february" and not leap_year:
print "28 days"
elif month == "march":
print "31 days"
elif month == "april":
print "30 days"
elif month == "may":
print "31 days"
elif month == "june":
print "30 days"
elif month == "july":
print "31 days"
elif month == "august":
print "31 days"
elif month == "september":
print "30 days"
elif month == "october":
print "31 days"
elif month == "november":
print "30 days"
elif month == "december":
print "31 days"
else:
print 'unknown month'
I would suggest you to using dictionary which a build-in data type in python.
def get_days(year, month):
"""
take year and month ,return the days in that month.
"""
#define a dictionary and the month is key which value is days
daydict = dict()
daydict = ['january'=31, 'february'=28, 'februaryinleapyear'=29,
'march'=31, 'april'=30,
'may'=31, 'june'=30,
'july'=31, 'august'=31,
'september'=30, 'october'=31,
'november'=30, 'december'=31 ]
try:
if month in daydict:
if month == 'february' and leap_year(year):
print daydict[februaryinleapyear]
else:
print daydict[month]
else:
print 'The year or month you input is invalid !'
except:
print 'error!'
n=int(input('enter no of days in a month= '))
#0=sunday
#1=moday
#2=tuesday
#3=wednesday
#4=thursday
#5=friday
#6=saturday
d=int(input('enter the starting day of month= '))
print('sun','mon','tue','wed','thu','fri','sat',sep='\t')
for j in range(d):
print (' ',end='\t')
i=1
while(i<=n):
print (i,end='\t')
if(i+d)%7==0:
print('\t')
i=i+1
This code ask for user inputs( more simple codes)
#python calendar days in month.
month= input ("Enter the month('January', ...,'December'): ") # ask for inputs from user
start=input ("Enter the start day ('Monday', ..., 'Sunday'): ")
if start== "Monday" :
num=1
elif start== "Tuesday" :
num=0
elif start== "Wednesday" :
num=-1
elif start== "Thursday" :
num=-2
elif start== "Friday" :
num=-3
elif start== "Sartday" :
num=-4
elif start=="Sunday":
num=-5
print(month)
print("Mo Tu We Th Fr Sa Su") #the header of the Calender
if month== "January" or month=="March" or month=="May" or month=="July" or month=="August" or month=="October" or month=="December" :
#for month with 31 days
for num_ in range (num,num+41,7):
for i in range(7):
if num<1:
print (' ',end="")
elif num>31:
print("",end="")
else:
print ("{0:>2}".format(num),end="")
if i<6 and num<31:
print(" ",end="")
num +=1
print()
elif month== "April" or month=="June" or month=="Septemmber" or month=="November":
#for month with 30 days
for num_ in range (num,num+41,7):
for i in range(7):
if num<1:
print (' ',end="")
elif num>30:
print("",end="")
else:
print ("{0:>2}".format(num),end="")
if i<6 and num<30:
print(" ",end="")
num +=1
print()
elif month== "February" :
# february is an exception : it has 28 days
for num_ in range (num,num+41,7):
for i in range(7):
if num<1:
print (' ',end="")
elif num>28:
print("",end="")
else:
print ("{0:>2}".format(num),end="")
if i<6 and num<28:
print(" ",end="")
num +=1
print()
else:
print("invalid entry")

Categories

Resources