Python Forum
Error with conditionals - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Error with conditionals (/thread-5696.html)

Pages: 1 2


Error with conditionals - RiceGum - Oct-17-2017

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?


RE: Error with conditionals - ichabod801 - Oct-17-2017

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.


RE: Error with conditionals - RiceGum - Oct-17-2017

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.


RE: Error with conditionals - ichabod801 - Oct-17-2017

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.


RE: Error with conditionals - RiceGum - Oct-17-2017

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?


RE: Error with conditionals - ichabod801 - Oct-17-2017

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].



RE: Error with conditionals - RiceGum - Oct-17-2017

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



RE: Error with conditionals - ichabod801 - Oct-17-2017

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':.


RE: Error with conditionals - RiceGum - Oct-17-2017

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?


RE: Error with conditionals - ichabod801 - Oct-17-2017

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.