This question already has answers here:
Why does non-equality check of one variable against many values always return true?
(3 answers)
Closed 1 year ago.
Feel like this should simply work but its not. Works when its simple if != but not when using or. I know I can do it the other way but this should work...Here is a sample
today = input('Enter Day of the week (Sun, Mon, Tue and etc) ')
if today != 'Sun' or today != 'Sat':
print ('Go to work!')
else:
print('Weekend!!')
It needs to be:
if today != 'Sun' and today != 'Sat':
print ('Go to work!')
Because it is always either not Sunday OR not Saturday. Even on Sunday it is not Saturday. So, your statement will always be true. But if it is both not Sunday AND not Saturday, then go to work.
Related
This question already has answers here:
String formatting: % vs. .format vs. f-string literal
(16 answers)
Closed 3 years ago.
I'm new to Python and we're currently learning how to use if/elif/else and as a exercise our prof. wants us to write a program that answers if a year is a leap year or not.I found a guide that shows and gives you a pretty good explanation on how to write such a program.
The code looks like this:
year = int(input("Please Enter the Year Number you wish: "))
if (year%400 == 0):
print("%d is a Leap Year" %year)
elif (year%100 == 0):
print("%d is Not the Leap Year" %year)
elif (year%4 == 0):
print("%d is a Leap Year" %year)
else:
print("%d is Not the Leap Year" %year
The only thing I am trying to figure out but haven't been able to find a good answer to is why the author uses print("%d this is a leap year" %year)
How come %d when the running the program doesn't show up as %d when inside a string?
Here %d inside the string is a format specifier which means that it will be replaced with the integer value provided after the end of the string.
It is just a placeholder for the year value.
%d means you are expecting an integer int32 after the string which in your case is the year after each print statement.
for the same example you cna use Format method as well
print ("is a Leap Year : {}".format(year))
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I made a birthday program in Python, the program ask from the user the month he were born in, and then calculate the months until the user have birthday.
For example, the user enter the number 5 and the current month is 2, the program output "You have 3 months until your birthday!".
I don't know how to calculate the months until he have birthday correct.
Example:
from datetime import datetime
def Birthday():
CurrentMonth = datetime.now().month
BornIn = input("What month were you born in ? - ")
result = int(BornIn) + CurrentMonth
if int(BornIn) == CurrentMonth:
print("You have already Birthday in this month!")
elif int(BornIn) > 12:
print("Invalid Input!")
else:
print("You have", result , "monthes until your Birthday!")
Birthday()
What mathematical action do I need to do, to calculate the months until his birthday?
Look at line 7, I used + to calculate but obviously it won't work.
Edit:
I need to do result = CurrentMonth - int(BornIn). This should fix the problem.
Your operation is not correct :
You should do:
result = (int(BornIn) - CurrentMonth)%12
Modulo is here to manage case when your birthday is in the next year. Without modulo you will have negative results (here you won't have any problem because we are in December so your birthday can't be in the 13th months)
This question already has answers here:
Is a specific timezone using DST right now?
(2 answers)
Python daylight savings time
(8 answers)
Closed 4 years ago.
I have code that I've put together to identify the exact hour the daylight savings time starts and when it ends. The problem is that I can't catch everything between those particular hours, on those dates, and say that yes it is within daylight savings time. My code for detecting the hours is as follows:
def getDaylightSavings(date, hourEnding):
# daylight saving's time switches
#print date.month, date.isoweekday(), date.day, hourEnding
if (date.month == 3 and date.isoweekday() == 7 and (date.day >= 8 and date.day <= 14) and hourEnding == 3): return 1
if (date.month == 11 and date.isoweekday() == 7 and (date.day >= 1 and date.day <= 7) and hourEnding == 3): return 2
This way I know what time adjustments to use. The problem is that if the time I pass into the function is 3/11/2018 4:00:00 it won't return as daylight savings whereas 3/11/2018 3:00:00 will be detected.
Is there a library I can use for determining daylight savings or something I can do here code-wise that will help?
This question is not a duplicate because I need to determine whether daylight savings time is in effect for any arbitrary time. Please help.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
new to teaching python to yr10's. This appears to work when telling the end user if their year is a leap year or not. But can someone please confirm if this code is working or the best way to do this. I realise there are probably different methods...Johnny:)
leapYear = int(input("what year is it?:"))
if (leapYear %4) == 0:
print ("Thats a leap year")
elif (leapYear %100)==0:
print ("thats not a leap year")
elif (leapYear % 400)== 0:
print ("Thats a leap year")
else:
print("thats not a leap year")
Here are three alternative ways to check for a leap year in python, with the 2nd method being an improved version of your own attempt:
1) Using calendar:
import calendar
calendar.isleap(year)
2) Similar to your own attempt but taking out the redundant steps and converting it into a method:
def is_leap_year(year):
if year % 100 == 0:
return year % 400 == 0
return year % 4 == 0
3) Check if the year provided has a a 29th of February using datetime:
import datetime
def is_leap_year(year):
try:
datetime.date(year, 2, 29)
except ValueError:
return False
return True
N.B. Try to teach your students to use snake_case in python code rather than camelCase.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
date=raw_input("Enter Date:")
month=raw_input("Enter Month:")
day=raw_input("Enter day:")
date1=raw_input("Enter Date:")
month1=raw_input("Enter Month:")
day1=raw_input("Enter day:")
int(date,month)
int(date1,month1)
int(day,day1)
d=date1-date
m=month1-month
da=day1-day
print d,m,da
trying this program but getting an error
an integer is required
The int() function doesn't work like that:
Firstly, it takes one argument (the second is optional and is the base, which is irrelevant in your case).
Secondly, you need to assign the result to a variable.
Thus
int(date,month)
should be
date = int(date)
month = int(month)
and so on.
Maybe you wanted it like this.
date=raw_input("Enter Date:")
month=raw_input("Enter Month:")
day=raw_input("Enter day:")
date1=raw_input("Enter Date:")
month1=raw_input("Enter Month:")
day1=raw_input("Enter day:")
date = int(date)
month = int(month)
date1 = int(date1)
month1 = int(month1)
day = int(day)
day1 = int(day1)
d=date1-date
m=month1-month
da=day1-day
print d,m,da
From the Subject "calculating age of a person in logical way in python without using built in functions" and the Solution approach is however not ok if inbuilt module are not used.
Approach:
You should get the current date first and then the date of birth of the person, and finally find the difference between them, Which you can get via multiple ways.
1) use the datetime or module and time inbuilt module (Recommended approach).
import datetime
now = datetime.date.today()
currentyr = now.year
else
2) use string way- the Way you approached is also going to return the sought output.
However the error is due to the fact int()- converts integer number represented as string to integer value, and raw_input() will return you a string.