IF statement Issues - 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: IF statement Issues (/thread-5442.html) |
IF statement Issues - Kongurinn - Oct-04-2017 #hey everyone I am a new student in Python. Below is some code working on IF statements. Please #refer to the comment question in the middle of the page. def main(): age = eval(input("Enter your age: ")) citizen = eval(input("Enter the amount of years you have been a citizen: ")) if age >= 30: if citizen >= 9: print("You can be a state senator!") if age >= 25: if citizen >= 7: print("You can also be a house representative!") #I understand that ELSE will only show up if the other IF statements are not triggered #but how would one arrange the code to allow ELSE to be triggered if an IF statement is #also being used? else: print("You are not eligible for senate.") print("You are also not eligible for the house.") main() #Please see previous comments RE: IF statement Issues - ichabod801 - Oct-04-2017 Especially with function definitions and if statements, indentation is essential in Python. Please repost your code with python tags so we can see the indentation. See the BBCode link in my signature below for instructions. RE: IF statement Issues - Kongurinn - Oct-04-2017 It is indented when I paste it in but when I post it the indentations aren't there. def main(): age = eval(input("Enter your age: ")) citizen = eval(input("Enter the amount of years you have been a citizen: ")) if age >= 30: if citizen >= 9: print("You can be a state senator!") if age >= 25: if citizen >= 7: print("You can also be a house representative!") #I understand that ELSE will only show up if the other IF statements are not triggered #but how would one arrange the code to allow ELSE to be triggered if an IF statement is #also being used? else: print("You are not eligible for senate.") print("You are also not eligible for the house.") main() This is a result I got from a specific input: Enter your age: 25 Enter the amount of years you have been a citizen: 7 You can also be a house representative! RE: IF statement Issues - ichabod801 - Oct-04-2017 Did you use python tags like I suggested? Did you read the instructions in the BBCode link I mentioned? RE: IF statement Issues - gruntfutuk - Oct-04-2017 I don't really understand the question you posed in your comment before the else. If statements can be nested, as you have done. If statements that follow one another (not nested, but at the same level) do not take account of any previous condition evaluations. You can set "flags" within if blocks that make it easier to to take account of something previously tested without having to repeat a complex test. A flag is just a boolean variable, set to True or False. Don't forget, you can also combine conditions in the test: a => 1 and a <= 10. RE: IF statement Issues - ichabod801 - Oct-04-2017 I'm not sure exactly what output you're looking for. But I'm thinking you want a separate else statement for each of the if statements that use citizen. RE: IF statement Issues - Kongurinn - Oct-04-2017 I should probably change my ELSE output but it should say I will not be able to be a senator. How would I add an else tag for just that so it still says I can be in the house of representatives. RE: IF statement Issues - nilamo - Oct-04-2017 Each if can have it's own else. >>> spam = 42 >>> if spam > 5: ... if spam > 10: ... if spam > 100: ... print("it's big") ... else: ... print("it's a little big") ... else: ... print("larger than 5, less than 10") ... else: ... print("less than 5") ... it's a little big |