0
print("Hello World")
myName = input("Whats your name?")
myVar = input("Enter a number: ")
print(myName)
print(myVar)

if(myName == "Ben" and myVar == 5):
    print("You are cool")

elif(myName == "Max"):
    print("You are not cool")

else:
    print("Nice to meet you")

Sorry I know I'm probably just looking at this wrong but I can't seem to figure it out. I am very new to Python and was watching a YouTube tutorial and it helped me create the above program.

I would expect that by me entering "Ben" for input 1 and then "5" for the second input, it would return by printing "you are cool".

However every time I attempt it, it returns the "Nice to meet you" which I thought is not meant to be returned without the previous parts of the if statement returning false.

I appreciate any help, I just want to have a thorough understanding of this before moving forward.

the.salman.a
  • 945
  • 8
  • 29
  • Just added "" around the number in the first if like so: – benmen10 Mar 15 '18 at 06:26
  • if(myName == "Ben" and myVar =="5") – benmen10 Mar 15 '18 at 06:27
  • This made it work, giving me the output I was trying to get, but in the video he did not use the quotation marks. What's the difference between using quotation marks around a number and not when interacting with input("") functions? – benmen10 Mar 15 '18 at 06:28
  • A user input is always casted to a string, even if a number or a Boolean is entered. If you know that `myVar` is definitely a number, you can always cast it to a number. Assuming you are expecting an integer, you should do `int(myVar) == 5`. – Terry Mar 15 '18 at 06:36
  • 1
    Possible duplicate of [How can I read inputs as integers?](https://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-integers) – eyllanesc Mar 15 '18 at 06:39

2 Answers2

2

You are going to the else part because myVar is a string and you are comparing it to int.

Either use

myVar = int(input("Enter a number: "))  #Convert input to int. 

or

myVar =="5"
Rakesh
  • 81,458
  • 17
  • 76
  • 113
1

In python when we take input through input() function, by default it's a string. You can check it's type by-

print(type(VARIABLE))

that's why your program doesn't work right, because of the condition on line 7. You are comparing string variable to int. Here is the modified program.

print("Hello World")
myName = input("Whats your name?")
myVar = int(input("Enter a number: "))
print(myName)
print(myVar)

if(myName == "Ben" and myVar == 5):
    print("You are cool")
elif(myName == "Max"):
    print("You are not cool")
else:
    print("Nice to meet you")

Also, since you're new to python try to learn more about data-types in python. Few links link1, link2, link3 that'll help in understanding data-types in python.

the.salman.a
  • 945
  • 8
  • 29