3 Replies - 487 Views - Last Post: 10 August 2016 - 08:00 AM Rate Topic: -----

#1 Flyinglotus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 03-August 16

If and else statements are not working correctly?

Posted 09 August 2016 - 06:02 PM

Hello I am new to python and I made a simple if, elif, else statement but whenever I punch in true variables it still sends me to the else statement saying "You are not ok" is this a problem with the code or do I have something wrong with the computer? The code is at the bottom.
print ("Hello World")
myName = input("What is your name? ")
myVar = input("Enter a number: ")
if(myName == "Alex" and myVar == 0):
	print("Alex is great")
elif(myName == "Bob"):
	print("Bob is ok")
else:
	print("You are not ok")
	
	



Is This A Good Question/Topic? 0
  • +

Replies To: If and else statements are not working correctly?

#2 lisperati  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 25
  • Joined: 16-February 13

Re: If and else statements are not working correctly?

Posted 09 August 2016 - 06:16 PM

Hi
myVar if you want to test it as an int needs to be coerced into an int by specifying int before the input command.
ie myVar = int(input("Enter a number: "))

also the usual way to specify string input for testing is to coerce the string to one case only not a mixture so you can test accurately. if you would test with 'bob' and assign all your string input for this variable as myName.lower() before the test.


Hope this helps
Was This Post Helpful? 1
  • +
  • -

#3 Flyinglotus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 03-August 16

Re: If and else statements are not working correctly?

Posted 09 August 2016 - 06:21 PM

Thank you! I was watching a youtube tutorial on this but on the video the person did not put in int inside his code.
Was This Post Helpful? 0
  • +
  • -

#4 Marbelous  Icon User is offline

  • D.I.C Head

Reputation: 42
  • View blog
  • Posts: 132
  • Joined: 12-November 12

Re: If and else statements are not working correctly?

Posted 10 August 2016 - 08:00 AM

View PostFlyinglotus, on 09 August 2016 - 08:21 PM, said:

Thank you! I was watching a youtube tutorial on this but on the video the person did not put in int inside his code.


Are you sure his 0 wasn't in quotes (making it a string)? This would work too:

if(myName == "Alex" and myVar == "0"): # The quotes make the integer zero a string which would compare properly.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1