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))
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
It's works on "Not leap year" but it doesn't work on "Leap year":
month=int(input("Enter a month: "))
year=int(input("Enter a year: "))
if year%4==0 and year>=0:
if year%100!=0:
year="Leap Year"
elif year%100==0:
if year%400==0:
year="Leap year"
else:
year="Not leap year"
elif year<0:
print("Please enter a valid number")
elif year%4!=0 and year>0:
year="Not leap year"
elif month<1 and month>12:
print("Please enter a valid number")
if year=="Leap year":
if month==2:
print("Number of days:29")
elif month==1 or 3 or 5 or 7 or 8 or 10 or 12:
print("Number of days:31")
else:
print("Number of days:30")
if year=="Not leap year":
if month==2 :
print("Number of days:28")
elif month==1 or 3 or 5 or 7 or 8 or 10 or 12:
print("Number of days:31")
else:
print("Number of days:30")
In leap years ı think it's going in if state and cant go from if statement. How can ı fix that. However in not leap years it works because its get in elif statment.
The problem is with this line:
year="Leap Year"
"Leap Year" is not the same as "Leap year" because string comparison is case sensitive in python as well as in most programming languages.
In general it's not great practice to hardcode strings in multiple places. As other people have mentioned you don't probably don't want to reuse the year var in case you need it again. You should try using a new var and saving a boolean (true or false) value. Maybe name it isLeapYear or something like that.
When you are declaring year="Leap Year", there are different spellings at different lines. At some point, it is "Leap Year" and at some point it is "Leap year". You need to be case sensitive in python. Try writing only "Leap year" everywhere and comparing year=="Leap year" in if statement
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.
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.
This question already has answers here:
String formatting: % vs. .format vs. f-string literal
(16 answers)
Closed 6 years ago.
I don't understand how this statement is creating a color in Python. I would really appreciate any clarification. I see that it must be substituting in the random numbers but that is as far as I get.
rgb = ('#%02X%02X%02X' % (random.randint(0,255),random.randint(0,255),random.randint(0,255)))
The use of % is the substitute for the variable. For example, if I have the following code,
name = input('Enter your name')
age = input('How old are you?'
print('Hello, %, you are % years old' % name, age)
exit(0)
the % next to "Hello," would be replaced with the value of the variable "name" (As decided by the "% name" which follows the string.
Enter your name: Harvey
Hello, Harvey, you are 18 years old
In your case, the % would be a series of random numbers, which are used to generate the hex colour.
This question already has answers here:
What does %c character formatting do in Python and it's use?
(3 answers)
Closed 6 years ago.
I'm trying to print variable c but whenever I do I get "Cost of electricity: $□". But if I print the variable c with print(c) I get "6.122448979591836". So it must be something about substituting the variable in with the % right? I'm using Python 3.5.2 btw. Also I need the number displayed to be 6.12 and using round(c, 2) does nothing. Is it because it's not an int?
# print #100 Cost of Electricity
print("#100 Cost of Electricity")
# get wattage
w = int(input("Enter wattage: "))
# get hours used
h = int(input("Enter number of hours used: "))
# get price per/kWh
p = float(input("Enter price per kWh in cents: "))
# calculate cost
c = w*h/(1000*p)
# print price
print("Cost of electricity: $%c" % int(c))
Not that it matters anymore but I figured why I was so confused. I wasn't using %c because it displayed based on the ASCII values. I thought I had to put %c because I thought I had to put %(variable name). I forgot that what you normally use is %s or %r and that the variable name goes in later. An old book I have for Python 2 says %r is for displaying raw data and %s is for displaying to users.
Use:
print("Cost of electricity: $%.2f" % c)
The 'f' flag is for floating point format. The '.2' says print 2 digits after the decimal.