0

Just starting out with python functions (fun_movies in functions.py) and I can't seem to get out (via "no" or False) once in the loop:

main_menu.py

from functions import *

def menu():
   print("Press 1 for movies.")
   print("Press 2 to exit.")
menu()

option = int(input("Input a number: "))

while option != 0:
#try:
   if option == 1:
       fun_movies()
   elif option == 2:
       print("Goodbye! ")
       break
   else:
       print ("Wrong input")

functions.py

global movies
movies = {}

def fun_movies():
   name = input("Insert movie name: ")
   genre = input("Input genre: ")
   movies [name] = [genre]

   a = True
   while a:
       query = input("Do you want to input another movie? (yes/no) ")
       if query == "yes":
           name = input("Insert movie name: ")
           genre = input("Input genre: ")
           movies_if = {}
           movies_if [name] = [genre]
           movies.update(movies_if)
       elif query == "no":
           break
       else:
           print ("Wrong input!")        
   return movies

Code works fine when not called via import. When called via import (in main_menu.py), it keeps asking for infinite movies even when I input a "no". I can't find a way to exit the loop. Initially I had a "pass" but that didn't work.

Thanks in advance!

5
  • In the else case you aren't checking the result before the next time the user is prompted for input. So you're ignoring the input to half the prompts (the ones in the else , but not the ones at the top of the loop). Commented Jul 3, 2022 at 17:00
  • Please update your question with the correctly formatted code. Commented Jul 3, 2022 at 17:01
  • Have you considered just deleting the second assignment to query, to leave only the one whose result is correctly processed? Commented Jul 3, 2022 at 17:01
  • Also, consider adding repl(query) to what you print in the "Wrong input!" message. Commented Jul 3, 2022 at 17:02
  • To get out of a loop you can use break to skip an iteration you can use continueszzs Commented Jul 3, 2022 at 17:05

2 Answers 2

1
global movies
movies = {}

def fun_movies():
    name = input("Insert movie name: ")
    genre = input("Input genre: ")


    movies [name] = [genre]

    a = True
    while a:
        query = input("Do you want to input another movie? (yes/no) ")
        if query == "yes":
            name = input("Insert movie name: ")
            genre = input("Input genre: ")
            movies_if = {}
            movies_if [name] = [genre]
            movies.update(movies_if)
        elif query == "no":
            a = False
        else:
            print ("Wrong input!")        
    return movies

A few things:

Firstly, you don't need a==True as this statement returns True when a is True and False when a is False, so we can just use a as the condition.

Secondly, only use the input at the start of the loop as you want to ask once per iteration

Thirdly, place your return outside the loop so you only return when a==False and you don't want to input another movie.

edit: main file>

from functions import *

def menu():
   print("Press 1 for movies.")
   print("Press 2 to exit.")
menu()

option = int(input("Input a number: "))

while option != 0:
   if option == 1:
       fun_movies()
   elif option == 2:
       print("Goodbye! ")
       break
   else:
       print ("Wrong input")
   option = int(input("Input a number"))
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for your time, however when I call the function from another file it keeps asking me (even after saying "no" to insert a new movie.
@fromstackoverflowimportall can you show your other file? Maybe you're calling the function more than once, as I have tested this function and it works fine.
I have updated the code that doesn't let me exit once added a movie.
@fromstackoverflowimportall you are not adding input to the loop, see my edited post
Game Development you are right, I would only add (to your solution) menu() before the input so the end user knows what his/her options are. Thanks for your patience and solutions!
0
global movies
movies = {}

def fun_movies():
    name = input("Insert movie name: ")
    genre = input("Input genre: ")
    movies[name]= genre
    a = True
    while a:
        query = input("Do you want to input another movie?   (yes/no) ")
        if query == "yes":
            name = input("Insert movie name: ")
            genre = input("Input genre: ")
            movies_if = {}
            movies_if [name] = genre
            movies.update(movies_if)       
        elif query == "no":
            break
              
        else:
            print ("Wrong input!")
            # continue
    return movies

print(fun_movies())

Hope It works for you!

2 Comments

Thanks for your time, I tried a break previously too, but when I call the function (fun_movies()) from another file (from functions import *) it keeps asking me (even after saying "no" to insert a new movie.
Welcome to Stack Overflow. Code is a lot more helpful when it is accompanied by an explanation. Stack Overflow is about learning, not providing snippets to blindly copy and paste. Please edit your answer and explain how it answers the specific question being asked. See How to Answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.