-2

I'm making a auto-pick program where it will pick a specific tv show or movie depending on whether you want to watch a movie or a tv show. The first if statement picks a movie and the elif statement picks a tv show. Even though I input the statement to run elif, it will always run if, and never run elif.

>answer=input('Welcome to Showpicker. First off, would you like to watch a movie or a TV show? '  )
    if answer := 'Movie''movie':
         import time
         import random
         print('Great choice!')
         time.sleep(1)
         print('I will now pick a movie for you to watch!')  
         movie = ['The Hunt','Atonement','Da Vinci Code','The Menu','Dead Poets Society','Harry Potter      `Series','The Shining','Exorcist','1917','Maurice','Call Me by Your Name','The Father','Fantastic Beasts Series','James Bond Series','Dune','Dune 2','Indiana Jones Series','Drive','Blade Runner    Series','John Wick Series','Mission Impossible series','Drive','The Name of the Rose']
         time.sleep(3)
         print(random.choice(movie))
         time.sleep(3)
         print("Enjoy!Don't forget your popcorn.")
         exit()
    elif answer:='TV show''TVshow''tv show''tvshow':
        import time
        import random
        print('Great choice!')
        time.sleep(1)
        print('I will now pick a TV show for you to watch!')
        TV = ['Hannibal','Peaky Blinders','House MD','Stranger Things','Breaking Bad','The Mentalist']
        time.sleep(3)
        print(random.choice(TV))
        time.sleep(1)
        print("Enjoy!Don't forget your popcorn.")
        exit()  

As a result, it will always show the movie option even though whether I input Tv show or movie.

  • 4
    `answer := 'Movie''movie'` is an assignment, not a test, which you'd have seen if you'd done some basic debugging with `print`! You want `if answer in ('Movie', 'movie'):` – Swifty Jul 16 '23 at 06:26
  • Now I get the problem, and my program works. Thank you so much for helping! – Silverfish Jul 16 '23 at 06:37

1 Answers1

1
if answer := 'Movie''movie':

The := operator (since Python 3.8) assigns 'Movie''movie' (which is equivalent to 'Moviemovie' to answer. Because 'Moviemovie' is always true, this assignment expression is always true and this always runs.

You likely meant to test if answer is equal to either 'Movie' or 'movie'. E.g.

if answer == 'Movie' or answer == 'movie':

Or:

if answer in ('Movie', 'movie'):

Or even more flexible with regards to spelling:

if answer.lower() == 'movie':
Chris
  • 26,361
  • 5
  • 21
  • 42