Python Forum

Full Version: Error with conditionals
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
mes = input()

if mes in ("Abril", "Junho", "Setembro", "Novembro"):
print("30 dias")
elif mes in ("Janeiro", "Março", "Abril", "Maio", "Julho", "Agosto", "Outubro", "Dezembro"):
print("31 dias")
elif mes not in ("Janeiro", "Março", "Abril", "Maio", "Julho", "Agosto", "Outubro", "Dezembro", "Abril", "Junho", "Setembro", "Novembro", "Fevreiro"):
print("Não conheço este mês!")

if mes == "Fevreiro":
n = int(input())
if n%400 == 0 or n%4 == 0 and n%100 != 0:
print ("29 dias")
else:
print("28 dias")
I want the program to only ask me for n when mes = "February", it is running well but giving me error when i input the other strings. What is my mistake?
It is hard to say. This could be a problem of indentation. Please respost your code using Python tags, so we can see the indentation. See the BBCode link in my signature for instructions.
mes = input()

if mes in ("Abril", "Junho", "Setembro", "Novembro"):
print("30 dias")
elif mes in ("Janeiro", "Março", "Abril", "Maio", "Julho", "Agosto", "Outubro", "Dezembro"):
print("31 dias")
elif mes not in ("Janeiro", "Março", "Abril", "Maio", "Julho", "Agosto", "Outubro", "Dezembro", "Abril", "Junho", "Setembro", "Novembro", "Fevreiro"):
print("Não conheço este mês!")

if mes == "Fevreiro":
n = int(input())
if n%400 == 0 or n%4 == 0 and n%100 != 0:
print ("29 dias")
else:
print("28 dias")

NameError: name 'n' is not defined on line 12 in main.py this appears with every string except fevreiro which is the string i want it to run the n variable.

I want a program to read a name of a month than tell me the number of days, in case of being february it should ask me to imput the year to tell me if there are 28 or 29 days.
Again, you need to use Python tags when posting code. I'm guessing that your last if/else needs to be indented so it's under the Fevreiro if statement. But I can't tell because I can't see the indentation of your code. Click on the BBCode link in my signature below for instructions.
I don't understand the instructions. Can you help me? I would really apreciate it. The code is as you see it, can you run it on python and try to see what's wrong?
I can't run your program as I see it, because I can't see the indentation.

Output:
Before your code put [python], then paste your code with control-shift-v, and then put [/python].
mes = input()

if mes in ("Abril", "Junho", "Setembro", "Novembro"):
print("30 dias")
elif mes in ("Janeiro", "Março", "Abril", "Maio", "Julho", "Agosto", "Outubro", "Dezembro"):
print("31 dias")
elif mes not in ("Janeiro", "Março", "Abril", "Maio", "Julho", "Agosto", "Outubro", "Dezembro", "Abril", "Junho", "Setembro", "Novembro", "Fevreiro"):
print("Não conheço este mês!")

if mes == "Fevreiro":
n = int(input())
if n%400 == 0 or n%4 == 0 and n%100 != 0:
print ("29 dias")
else:
print("28 dias")
i´m sorry for the trouble. literaly just put that? I see no diference in the format!?

mes = input()
 
if mes in ("Abril", "Junho", "Setembro", "Novembro"):
 print("30 dias")
elif mes in ("Janeiro", "Março", "Abril", "Maio", "Julho", "Agosto", "Outubro", "Dezembro"):
 print("31 dias")
elif mes not in ("Janeiro", "Março", "Abril", "Maio", "Julho", "Agosto", "Outubro", "Dezembro", "Abril", "Junho", "Setembro", "Novembro", "Fevreiro"):
 print("Não conheço este mês!")
 
if mes == "Fevreiro":
 n = int(input())
if n%400 == 0 or n%4 == 0 and n%100 != 0:
 print ("29 dias")
else:
 print("28 dias")
If the second set of code you posted matches your actual indentation, the problem is the last four lines. They will run no matter what, but you only define n if it is Fevreiro. You only want them to run if it is Fevreiro. So indent those last four lines one time each. Then they will be in the block of code that is run if mes == 'Fevreiro':.
if n%400 == 0 or n%4 == 0 and n%100 != 0:
print ("29 dias")
else:
print("28 dias")
these lines? "So indent those last four lines one time each" how do i do this?
The same way you indented the lines after the previous if and elif statements. That is, line 4 (print("30 dias")) has some spaces or a tab character in front of it. Whatever it has before it, put that in front of those four lines.
Pages: 1 2