Conditional statement not working


#1

Good Morning

I have written the following code:

print("hello I am python")
myName == input("What is your name?")
print (myName)
myVar = input("When is your birthday: ")
if(myName == "Andy" and myVar == 1):
        print("Andy is wonderful!!")
elif(myName == "Helene"):
        print("hi helene")
else:
        print("hello")

If I enter ‘Andy’ and ‘1’ the program just prints ‘hello’. The program works if I enter ‘Helene’. If someone could please point out what I am doing wrong I would be very grateful!

Regards

Len


#2

I have indented the ‘if’, ‘elif’ and ‘else’ lines.


#3

here:

myName == input("What is your name?")

why would check if equal? I get an error already right there

if we do:

if(myName == "Andy")

everything goes fine (except the parentheses are redundant)

so, something goes wrong with myVar == 1. Are you certain of the data types in use?


#4

Hi I was running your code and i made some changes to it. Let me know if it works for you. I added a comment to where i made changes.

Solution one

print("hello I am python")

## changed == to =
myName = input("What is your name?")

print (myName)
## added int infront of this input since you are comparing myVar to a int
myVar = int(input("When is your birthday: "))

if(myName == "Andy" and myVar == 1):
    print("Andy is wonderful!!")
    
elif(myName == "Helene"):
    print("hi helene")
else:
    print("hello")

Solution two

print("hello I am python")

## changed == to =
myName = input("What is your name?")

print (myName)

myVar = input("When is your birthday: ")

##Change 1 to "1" so that I can compare a string value against a string
if(myName == "Andy" and myVar == "1"):
    print("Andy is wonderful!!")
    
elif(myName == "Helene"):
    print("hi helene")
else:
    print("hello")

Solution 3

print("hello I am python")

## changed == to =
myName = input("What is your name?")

print (myName)
## added eval infront of this input since you are comparing myVar to a int
myVar = eval(input("When is your birthday: "))

if(myName == "Andy" and myVar == 1):
    print("Andy is wonderful!!")
    
elif(myName == "Helene"):
    print("hi helene")
else:
    print("hello")

Output for both


hello I am python
What is your name?Andy
Andy
When is your birthday: 1
Andy is wonderful!!

Solution Explanation:

Your input value for myVar was reading as a string. So, you were comparing myVar (a string) to 1 (an int), which will not match the argument in the if statement. The solution was to either convert myVar as an int(shown in solution 1) or compare myVar to a string (shown in solution 2)

Let me know if this helps or you have additional questions. :slight_smile:

For Solution 3 please research eval and why you should not use it, in this instance. (P.S Or read the comments) Hint: Has to do with security!!


#5

security 101, using eval() will evaluate the input as code. This is quite a unnecessary security risk you are taking here


#6

Yes, it does, but I wasn’t necessarily thinking of it that way, but nice of you to bring it up. Using eval brings up wonder. Which leads to some research on eval. LOL.

He can just use:

myVar = int(input("When is your birthday: "))

in solution one, but that’s boring. And now they’ll never think of eval T_T lol


#7

security should always be on the mind of a developer, if you look at the number of security issues and data breaches over the last two years, its staggering and seriously worrying

i prefer boring over security risk any day.

They can learn about eval() later. But right now its more important to teach new learners the right way to convert to integer. Eval on user input is a no go anyway, so this not the right place nor time to teach eval.

Bad habits learned early can take a long time to get out of there system. So please make an edit to your answer.

lets end on a positive note, i do like the rest of your answer :slight_smile:


#8

When I wrote, “but I wasn’t necessarily thinking of it that way” I meant I just have a different teaching method. I like to leave cookies. Leave the leaner something to ponder about or a chance to do some personal research, especially new ones. After all, some learn best from mistakes and/or conducting their own research. If I was very new to python and it was my first time seeing eval; I personally would ask myself, what in the world is it and perform a google search. And like most google searches you’ll find additional information you weren’t looking for. Like the security issue with eval. LOL, next time I’ll defiantly put a disclaimer or a hint to why I’ll use something else. I completely agree with you though. Teaching security is important ^_^.


#9

Made the edits. Is that suggestion better?


#10

I took this one step further, i left them to ponder on the data types in use. Then the user has to do research, or ask for more help. You took out most of these thinking steps, by crafting most of the solution. Now, i am not saying what you do is wrong. It just differs from my perspective to leave as many possible thinking steps within my answer and help in small incremental steps.

true, but this might not apply to everyone, some people would just assume this is the way to convert to integer.


#11

We all have our own teaching methods and learning methods. Hopefully the user will be able to see where they went wrong and why. We’re both here to help which is awesome ^_^. I like to leave cookies, but I prefer to give detailed answers, because sometimes not everyone has a programmer mind set just yet.


#12

totally agree :slight_smile:

true, but sometimes it might be good to push them a little bit towards the programmers mindset. Although interesting, we can agree that there isn’t one definitive answer to that question.


#13

Agreed. In programming, there is no definitive answer to any one question. But that’s the fun and appeal of it. You can think of a hundred ways to do something. Plus it helps when you have communities like this one, because everyone here can teach you something new.