Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
If Statement
#1
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: ")
Reply
#2
check
https://python-forum.io/Thread-Multiple-...or-keyword

on a side note line 15 is redundant, it's not doing anything.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
#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:
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#4
@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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
@Bibcat, note that you dont account for UK citizens at age exactly 18.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
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')
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#7
@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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#8
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
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#9
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 :-)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#10
Sorry, was not my intention to solve homework for someone. Just trying to be helpful.
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply


Forum Jump:

User Panel Messages

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