0

I am not sure why this code is not working as supposed, can any one help me please ?

# check if a user eligible for watching coco movie or not.
# if user name starts with "a" or "A" and his age is greater then 10 then he can, else not.

user_name = input("please enter your name in letters here: ")
user_age = int(input("please enter your age in whole numbers here: "))

if user_name[0] == ("a" or "A") and user_age > 10 :
  print("you can watch coco movie")
else:
  print("sorry, you cannot watch coco")

I tested this code with every possible condition and it is working fine but with last condition is not working as supposed , in last condition don't know why condition is False.

I am pasting here all tested conditions and there results from IDLE :

please enter your name in letters here: a
please enter your age in whole numbers here: 9
sorry, you cannot watch coco
>>> 
====================== RESTART: D:\MyPythonScripts\1.py ======================
please enter your name in letters here: a
please enter your age in whole numbers here: 10
sorry, you cannot watch coco
>>> 
====================== RESTART: D:\MyPythonScripts\1.py ======================
please enter your name in letters here: a
please enter your age in whole numbers here: 11
you can watch coco movie
>>> 
====================== RESTART: D:\MyPythonScripts\1.py ======================
please enter your name in letters here: A
please enter your age in whole numbers here: 9
sorry, you cannot watch coco
>>> 
====================== RESTART: D:\MyPythonScripts\1.py ======================
please enter your name in letters here: A
please enter your age in whole numbers here: 10
sorry, you cannot watch coco
>>> 
====================== RESTART: D:\MyPythonScripts\1.py ======================
please enter your name in letters here: A
please enter your age in whole numbers here: 11
sorry, you cannot watch coco
>>> 
DYZ
  • 55,249
  • 10
  • 64
  • 93

2 Answers2

1

In the code that you wrote, user_name[0] is compared to the expression ('a' or 'A'). The expression ('a' or 'A') evaluates to 'a'. Try this,

print( 'a' or 'A' )

The result is

a

Therefore, the condition tests true only when user_name begins with 'a' and age is greater than 10.

Here is a code snippet that does what you perhaps intended:

if user_name[0] in 'aA' and user_age > 10 :
  print("you can watch coco movie")
else:
  print("sorry, you cannot watch coco")
DrM
  • 2,404
  • 15
  • 29
0

The reason your last test case wasn't getting the expected value was this condition

user_name[0] == ("a" or "A")

You see ("a" or "A") is being evaluated differently than you think. The parenthesis make this it's own expression. That expression basically says If 'a' is null then return 'A'

This always returns 'a' therefore, returning that output.

user_name[0] == "a" or user_name[0] == "A"

Should fix this

Look into this post for greater explaination https://stackoverflow.com/a/13710667/9310329

Cheers

  • 1
    To avoid indexing twice: `user_name[0] in ('a', 'A')` – Brad Solomon Jun 24 '18 at 02:47
  • @ Brad Solomon. Thank you very much sir , now i understand what mistake was i making and also the alternate ways to achieve this goal. one more question sir. What if i change the condition from "('a' or 'A')" to " 'a' or 'A' " ? – Ilmedin zeeshan Jun 24 '18 at 03:01
  • 1
    @BradSolomon To avoid tuple construction: `user_name[0] in 'aA'`. – DYZ Jun 24 '18 at 03:05