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: ")
#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 :
Output:
Enter your name: Jack
Enter your age: 20
Enter country of citizenship: USA
Sorry you are not entitled to vote:
@Bibcat, note that you dont account for UK citizens at age exactly 18.
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')
@
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
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')
Output:
Enter your name: john
Enter your age: 18
Enter your country USA
USA is not entitled to vote
Enter your name: john
Enter your age: 15
Enter your country UK
15 is not old enough to vote.
You are entitled to vote
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 :-)
Sorry, was not my intention to solve homework for someone. Just trying to be helpful.