If Statement - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: If Statement (/thread-26463.html) Pages:
1
2
|
If Statement - Bobcat - May-02-2020 Hi. I'm struggling with my else statement to print 'Sorry you are not entitled to vote due to being outside the uk' #Ask user for details name = (input("Enter your name: ")) age = int(input("Enter your age: ")) country = (input("Enter country of citizenship: ")) #Check if eligible to vote if age <18: print (age, "You are not yet old enough to vote: ") #If over 18 and from Britain or UK you are entitled to vote elif age >18 and country == "Britain" or "UK": print ("You are entitled to vote: ") #Otherwise else: country !="Britain" or "UK" print ("Sorry you are not entitled to vote: ") RE: If Statement - buran - May-02-2020 check https://python-forum.io/Thread-Multiple-expressions-with-or-keyword on a side note line 15 is redundant, it's not doing anything. RE: If Statement - pyzyx3qwerty - May-02-2020 #Ask user for details name = (input("Enter your name: ")) age = int(input("Enter your age: ")) country = (input("Enter country of citizenship: ")) #Check if eligible to vote if age <18: print (age, "You are not yet old enough to vote: ") #If over 18 and from Britain or UK you are entitled to vote elif age >18 and country == "Britain" or country == "UK": print ("You are entitled to vote: ") #Otherwise elif country != "Britain" or country !="UK": print ("Sorry you are not entitled to vote: ")Fixed it use country == "Britain" or country == "UK" instead of elif age >18 and country == "Britain" or "UK": output :
RE: If Statement - buran - May-02-2020 @pyzyx3qwerty, probably you should also read https://python-forum.io/Thread-Multiple-...or-keyword and adding if on line 15 does not make it more useful.
RE: If Statement - buran - May-02-2020 @Bibcat, note that you dont account for UK citizens at age exactly 18. RE: If Statement - menator01 - May-02-2020 My 2 cents #! /usr/bin/env python3.8 '''Docstring''' name = input('Enter your name: ') age = input('Enter your age: ') country = input('Enter your country ') if age < str(18) or country != 'Britain' or country != 'UK': print(f'You are not entitled to vote') else: print('You are entitled to vote') RE: If Statement - buran - May-02-2020 @menator01 - this will not work, because at least one of the conditions country != 'Britain' or country != 'UK' will be True, thus it will always print You are not entitled to vote
RE: If Statement - menator01 - May-02-2020 What about this take? Seems to work on my box. name = input('Enter your name: ') age = input('Enter your age: ') country = input('Enter your country ') if age < str(18): print(f'{age} is not old enough to vote.') if country not in ['Britain', 'UK']: print(f'{country} is not entitled to vote') else: print('You are entitled to vote')
RE: If Statement - buran - May-02-2020 on line 9 it's better to have elif , not if .The way you have it now will print 2 sentences if age < 18 and country not in ['Britain', 'UK'] - not able to vote because of age and not able to vote because of country As a side note to both you and @pyzyx3qwerty - this very much looks like homework and that is why I just provided guidance to OP, i.e. I didn't want to give away ready solution. Anyway you did :-) RE: If Statement - menator01 - May-02-2020 Sorry, was not my intention to solve homework for someone. Just trying to be helpful. |