Python Forum
Error with conditionals
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error with conditionals
#1
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?
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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.
Reply
#4
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
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?
Reply
#6
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].
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
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")
Reply
#8
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':.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#9
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?
Reply
#10
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Functions with conditionals and comparison operators Drone4four 9 12,923 Jan-01-2019, 06:48 PM
Last Post: Drone4four
  Nested Conditionals Elero 3 7,932 Apr-16-2018, 07:58 PM
Last Post: Elero

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020