【问题标题】:How to use variable defined inside main.py file into another file?如何将 main.py 文件中定义的变量用于另一个文件?
【发布时间】:2021-12-05 01:34:54
【问题描述】:

这是我作为程序员的第一个月,所以我正在创建一个电影预订网站的副本。我在main.py 文件中写了一些代码:

def main():
    current_income=0
    print('---->Enter the number of row in Cinemahall :- ')
    while True:
        try:
            row=int(input())
            break
        except:
            print('-->Something went wrong!! Please enter the valid row in cinemahall. The value must be integer type :')
    print("---->Enter the number of column in Cinemahall :- ")
    while True:
        try:
            col=int(input())
            break
        except:
            print('-->Something went wrong!! Please enter the valid column in cinemahall. The value must be integer type :')

    while True:
        import options_movies
        options_movies.options()
        break

if __name__=='__main__':
    main()

现在我有另一个文件options.py:

def options():

    while True:
        print('1. show the seats')
        print('2. buy a ticket')
        print('3. statistics')
        print('4. show booked ticked user info')
        print('0. exit')
        print('--> Please select one option from 1,2,3,4,0 ')
        ##try:
        n=int(input())
        if n==1:
            from main import main
            import show_seats
            show_seats.show_the_seats(main.row,main.col)
        elif n==2:
            import buy_ticket
            buy_ticket.buy_a_ticket()
        elif n==3:
            import statistics
            statistics.statistics()
        elif n==4:
            import user_info
            user_info.booked_ticket_user_info()
        elif n==0:
            print('Thank you for using BOOK MY SHOW,We hope you will enjoy the show...... Please visit again!!')
            break
                ##assert n>=0 and n<=4
       ## except:
           ## print('Something went wrong!!!! Please enter the valid option from 1,2,3,4,0')

现在,当我尝试从 main.py 文件中使用 show_seats.show_the_seats() 中的 row 和 col 值时,我收到此错误

  File "C:\Users\ROHIT KUMAR VERMA\OneDrive\Documents\Book My Show Project\main.py", line 23, in <module>
    main()
  File "C:\Users\ROHIT KUMAR VERMA\OneDrive\Documents\Book My Show Project\main.py", line 20, in main
    options_movies.options()
  File "C:\Users\ROHIT KUMAR VERMA\OneDrive\Documents\Book My Show Project\options_movies.py", line 16, in options
    show_seats.show_the_seats(main.row,main.col)
AttributeError: 'function' object has no attribute 'row'

【问题讨论】:

  • 请修正你的缩进。
  • import 语句应位于脚本的开头。带有无条件 breakwhile 循环不是循环。您的函数需要参数来获取所需的值,并需要 return 来从函数中返回 vakues。总而言之:退后一步,了解导入和函数的工作原理。
  • @Matthias 我在任何 if 或 elif 条件满足时导入模块。只有当我输入 0 时,while 循环才会中断。此外,我已经尝试了你所说的所有方法,但问题仍然存在..

标签: python import module main


【解决方案1】:

在函数内部创建的变量只能在该函数内部使用。 您需要创建全局变量:

my_variable = "awesome"

def myfunc():
  print("Python is " + my_variable)

myfunc()

现在,您可以将变量导入您需要的文件中:

from filename import variable

参考:

【讨论】:

  • 您的回答将导致使用global,而在大多数情况下使用global 是一个非常非常糟糕的主意。
  • 据我了解,他不想修改变量
  • @FelipeCarvalho 感谢您的建议,但 row 和 col 正在从用户那里获取价值,我在 7 个模块中使用该值。当我在没有if __name__=='__main__:` 的情况下编写 main.py 文件时,我能够实现我想要的,但是当我导入主文件时这样做,它会被执行,而不仅仅是导入变量。如果我使用if __name__=='__main__:` 我会收到此错误。我希望我这次更清楚。
猜你喜欢
  • 2013-01-31
  • 1970-01-01
  • 2020-05-02
  • 2011-02-19
  • 1970-01-01
  • 2018-01-10
  • 1970-01-01
  • 1970-01-01
  • 2016-07-10
相关资源
最近更新 更多