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