简体   繁体   English

无法传递/退出 python 函数

[英]Unable to pass/exit a python function

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:刚开始使用python函数(functions.py中的fun_movies),我似乎无法在循环中退出(通过“no”或False):

main_menu.py主菜单.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函数.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".当通过导入(在 main_menu.py 中)调用时,即使我输入“否”,它也会不断要求无限电影。 I can't find a way to exit the loop.我找不到退出循环的方法。 Initially I had a "pass" but that didn't work.最初我有一个“通行证”,但这没有用。

Thanks in advance!提前致谢!

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.首先,您不需要a==True ,因为该语句在 a 为 True 时返回 True,在 a 为 False 时返回 False,因此我们可以只使用 a 作为条件。

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.第三,将您的 return 放在循环之外,这样您只在 a==False 并且您不想输入另一部电影时才返回。

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"))
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!希望这对你有用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM