Posts: 1
Threads: 1
Joined: May 2020
May-02-2020, 03:28 PM
(This post was last modified: May-02-2020, 03:33 PM by buran.)
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: ")
Posts: 8,160
Threads: 160
Joined: Sep 2016
May-02-2020, 03:36 PM
(This post was last modified: May-02-2020, 03:36 PM by buran.)
check
https://python-forum.io/Thread-Multiple-...or-keyword
on a side note line 15 is redundant, it's not doing anything.
Posts: 353
Threads: 13
Joined: Mar 2020
May-02-2020, 03:55 PM
(This post was last modified: May-02-2020, 04:07 PM by pyzyx3qwerty.)
#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:
Posts: 8,160
Threads: 160
Joined: Sep 2016
@ 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.
Posts: 8,160
Threads: 160
Joined: Sep 2016
@Bibcat, note that you dont account for UK citizens at age exactly 18.
Posts: 1,145
Threads: 114
Joined: Sep 2019
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')
Posts: 8,160
Threads: 160
Joined: Sep 2016
@ 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
Posts: 1,145
Threads: 114
Joined: Sep 2019
May-02-2020, 07:47 PM
(This post was last modified: May-02-2020, 07:47 PM by menator01.)
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
Posts: 8,160
Threads: 160
Joined: Sep 2016
May-02-2020, 07:57 PM
(This post was last modified: May-02-2020, 07:57 PM by buran.)
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 :-)
Posts: 1,145
Threads: 114
Joined: Sep 2019
Sorry, was not my intention to solve homework for someone. Just trying to be helpful.
|