从导入的文件调用函数时发生AttributeError

时间:2019-05-15 14:04:24

标签: python function import

我正在尝试从另一个文件导入功能。据我了解,我要做的就是导入和使用。

我对类不是很熟悉,但是试图弄乱这个函数并使它成为一个类。那没用。我也尝试从文件导入功能。一切都在一个文件中运行良好,但它变得太大了,我想将代码组织到单独的文件中。调用tarotDict(只有三个长字典)可以正常工作,但是当我尝试调用函数时,就被AttributeErrors击中。

fortune.py

import tarotFunctions

if user_selection == "1":
    print("-------------------------")
    print("Please select how many cards you wish to draw.")
    print("1. Single card draw.")
    print("2. Three card draw (Past, Present, Future)")
    print("3. Card information")
    print("0. Main menu")
    user_tarot_selection = input("> ")
        if user_tarot_selection == "1":
            tarotFunctions.draw_one() #<----------------------- 
                                      #attribute error when calling

tarotFunctions.py

import random
import tarotdict

def draw_one():
    single_draw = random.randint(1,78) 
    single_up_or_down = random.randint(1,2) 
    print("-------------------------")
    print("You have drawn the " + tarotdict.tarot[single_draw] + " 
          card.")
    if single_up_or_down == 1: #determine if face up or down
        print("Your card is facing up and represents:")
        print(tarotdict.tarot_face_up[single_draw])
        print("-------------------------")
    else:
        print("Your card is facing down and represents:")
        print(tarotdict.tarot_face_down[single_draw])
        print("-------------------------")



Traceback (most recent call last):
File "fortune.py", line 35, in <module>
main_menu()
File "fortune.py", line 20, in main_menu
tarotFunctions.draw_one() 
AttributeError: module 'tarotFunctions' has no attribute 'draw_one'

1 个答案:

答案 0 :(得分:0)

您的draw_one似乎有一个错误。
无法通过这种方式完成以下

print("You have drawn the " + tarotdict.tarot[single_draw] + " 
          card.")

它必须是:

print("You have drawn the " + tarotdict.tarot[single_draw] + " card.")

print("You have drawn the " + tarotdict.tarot[single_draw] + " \
          card.")

print("You have drawn the " + tarotdict.tarot[single_draw] 
    + " card.")

如果没有\,则不能使用多行引号(例如,字符串)。