Ask coding questions

← Back to all posts
Why if I choose "cat" or "hamster" there is always one answer?
DeepGreen (8)

For example, if you select "cat", "lol" should be displayed, and if "hamster" is selected, "hello" should be displayed.

Answered by Bookie0 (3694) [earned 5 cycles]
View Answer
Commentshotnewtop
Bookie0 (3694)

hi there!

so what you are doing is first assigning Alyx to an input. that is fine. but then, you make Alyx equal to hamster, then cat, then dog.

so in the end, Alyx = dog.

so the input doesnt matter anymore, since you changed the variable.

then the if statement that you use will always print gav gav because Alyx is always = dog.

so what i suggest you do, if you want that if the user enters 'cat' and then it outputs 'lol', then just remove lines 3, 4, and 5.

maybe this is what you want it to look like:

yagami = input("Hi! \nWhat pet do you want?")
Alyx = input("hamster, cat, dog: ")

# Alyx="hamster"
# Alyx="cat"
# Alyx="dog"
# the hashtag is for commenting; whatever is after the hashtag isnt executed by the program ;)
if (Alyx=="hamster"):
  print("hello")
elif (Alyx=="cat"):
  print("lol")
elif (Alyx=="dog"):
  print("gav gav")

Is this what you want? if not, tell me and i will try to help you!

Happy coding! =)

Bookie0 (3694)

sure happy to help! ;) @DeepGreen

PattanAhmed (1014)

@DeepGreen Hi,
Here is the correct code to be written:-

yagami = input("Hi! \nWhat pet do you want?(hamster, cat, dog): ")

if yagami == "hamster":
  print("hello")
elif yagami =="cat":
  print("lol")
elif yagami =="dog":
  print("gav gav")
else:
  print('Please type in only the pets given in the selection')

Hope this helps
Please mark my answer if this helps

PattanAhmed (1014)

@DeepGreen I am Glad that you got helped
Please mark my answer if this helps :)

PattanAhmed (1014)

@DeepGreen Or at least upvote all my comments here!
Thanks a lot

Coder100 (6992)

Hi! That's because you are comparing Alyx which has the value dog. Instead, have your code be:

yagami = input("Hi! \nWhat pet do you want?")
Alyx = input("hamster, cat, dog: ")
Alyx="hamster"
Alyx="cat"
Alyx="dog"
if (yagami=="hamster"):
  print("hello")
elif (yagami=="cat"):
  print("lol")
elif (yagami=="dog"):
  print("gav gav")

which takes user input

DEMONul1234 (43)

You've made an error by putting Alyx thrice in a row and giving it a different string each time. Since python understands the last Alyx string, it'll always give you the same answer. Try doing sth like:

yagami = input("Hi! \nWhat pet do you want?")
if yagami == "hamster":
print("Hello")
It'll make you code look a whole lot of better.
Alternatively, you can do sth like this:
yagami = input("Hi! \nWhat pet do you want?")
x = "hamster"
y = "cat"
z = "dog"
if yagami == y:
print("lol")