Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

The if statement runs even though I input the statement to run elif

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 option is the movie and the elif option is the tv show option. Even though I input ‘Tv show’, it will always pick a movie, and never the TV show option.

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()

Whether I input Movie or TV show, it will always show the movie option.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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':
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading