How can ı fix that if else problem Python? [closed] - python

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

Related

Input function help (python) [duplicate]

This question already has answers here:
How can I read inputs as numbers?
(10 answers)
Closed 2 years ago.
I was trying to play around with the input function. However, it seems that this code is not correct, I looked to see examples online but they tend to be basic and what i was trying to do was slightly above basic. Also, is it possible to put the return function instead of the print one? if not why? I'm new to this so if these all sound stupid please forgive me.
cheers!
def user_data(x):
age = input("how old are you?")
if age == 20:
print("you win")
else:
print("you lose")
input returns a string, while you are comparing age (a string) to an integer.
You have to either compare age to a string (so age == "20"), or convert age to an int (so int(age) == 20).
See docs to find out how input works.
def user_data(age):
if age == 20:
print("you win")
else:
print("you lose")
age = int(input("how old are you?"))
user_data(age)

Use of % in a string [duplicate]

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))

Is there a way I can calculate the months until user's birthday? [closed]

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)

leapyear python task for students [closed]

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.

Python Invalid Syntax: Print and Elif statements [closed]

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 7 years ago.
Improve this question
I am really new to Python and programming (2 days to be exact). I was messing around in idle trying to come up with a more complicated process than just print "Hello world." I was wondering if any of you could tell me why it is marking my print and elif statements invalid. I am using Python 2.7.10, thanks!
A = raw_input("Do you live in the US or Canada?")
if A == " US" or "Canada":
print "Welcome!"
else:
print "We're sorry, but your country is currently not supported!"
B = int(raw_input("How much is your package?")
if B >= 25
print "Your shipping is $4.00"
elif B >= 50
print "Your shipping is $8.00"
else:
print "Congrats, your shipping is free!"
Probably the first thing you notice about python is that consistent indentation is not just a good idea, it is mandatory. Experienced programmers, whether they write Python or not, always do that anyway so it is no big deal. Use spaces (4 is the norm) and avoid tabs - change your editor to replace a tab with 4 spaces.
It is a bad idea to use float on money amounts because of rounding. Better to use a large type, like Decimal, or store the amount as an int in cents, then insert a decimal point when you display. For simplicity I have stuck with using float, but be warned.
You have a number of logic errors in your code, as well as issues with style. Programming style is not just about what looks nice, it is whether you can understand your code later, when you come back to it.
Style points:
Don't use UPPERCASE for variables. By convention UPPERCASE is reserved for constants
Use meaningful variable names, not A and B
Here is a corrected program, with comments. Please read the comments! :
# This is a comment, it is ignored by python
# This is used later on by sys.exit()
import sys
# Logically the user would enter "Yes" or "No" to this quesion,
# not US or Canada!
ans = raw_input("Do you live in the US or Canada? ") # Notice the space after ?
# Note how the condition has been expanded
if ans == "US" or ans == "Canada":
print "Welcome!"
else:
print "We're sorry, but your country is currently not supported!"
# Now what? Your program just carried on. This will stop it
sys.exit()
# I'm using a floating point number for simplicity
amount = float(raw_input("How much is your package? "))
# I changed this around, since 50 is also >= 25!
# However, this is strange. Usually the more you spend the LESS the shipping!
# You were missing the : after the condition
if amount >= 50:
print "Your shipping is $8.00"
amount += 8 # This adds 8 to the amount
elif amount >= 25:
print "Your shipping is $4.00"
amount += 4 # This adds 4 to the amount
else:
print "Congrats, your shipping is free!"
# print the amount showing 2 decimal places, rounding
print "Amount to pay: $%.2f" % (amount)
You have plenty more to do. Maybe cope with the user entering lower or mixed case letters for the country name - and ask yourself if the question is logical to the user.
Later you might want to have a list of valid countries, and use in to test if the user entered a valid country. Then expand it to use a dictionary, indicating currency symbols, shipping amounts, and currency conversion rates for each country.
Enjoy Python!
Fixed it for you :). Take a look to see what's different, and you will learn, young padawan:
A = raw_input("Do you live in the US or Canada?")
if A == "US" or A == "Canada":
print "Welcome!"
else:
print "We're sorry, but your country is currently not supported!"
B = float(raw_input("How much is your package?"))
if B >= 25:
print "Your shipping is $4.00"
elif B >= 50:
print "Your shipping is $8.00"
else:
print "Congrats, your shipping is free!"

Categories