从模块导入类,但是函数无法工作。

2024-10-02 02:36:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我创建了两个程序,一个是ATM模拟器,另一个是虚拟医生,它从excel表格中读取数据,根据用户的输入,告诉用户可能患有什么疾病

现在我想把自动取款机连接到虚拟医生,这样它就可以从银行账户中提取药品的金额

我将atm导入了virtualdoctor,但是这些函数似乎不起作用,它们在被调用时什么都不做,进程退出

#code for ATM
userpin = ["1234", "2345", "3456", "4567"]
userpass = ["1234", "2345", "3456", "4567"]
username = ["Rishabh", "Siddharth", "Kashish", "Mahima"]
userbalance = [20500, 43567, 45672, 67800]

class Bank:

    def __init__(self, bal=0, index=0):
        #if __name__ == '__main__':
            self.bal = bal
            self.index = index

    def start(self):
        if __name__ == '__main__':
            print("\t\t=== Welcome to ATM ===")

            inputpin=input("Enter your pin :")
            inputpass= input("Enter your password :")
            b1.pinpasscheck(inputpin,inputpass)

    def pinpasscheck(self,pin,passw):
        self.pin=pin
        self.passw=passw
        inputpin=pin
        inputpass=passw
        index=0
        flag= False
        for i in range(0,len(userpin)):
            if inputpin==userpin[i]:
                index=i
                print(index)
                if inputpass==userpass[index]:
                    print("Login Succeeded !")
                    flag= True
                    b1.operationlist(index)
                    if flag==False:
                        print("Login invalid. Please check username or password")
                    else:
                        pass
                else:
                    pass

    def operationlist(self,indexval):

        self.indexval=indexval
        index=indexval
        print("\n Hello, ", username[index])
        print("""
                    1)        Balance
                    2)        Withdraw
                    3)        Deposit
                    4)        Change password
                    5)        Quit
                    """)
        useroption = int(input("Select an option:"))

        if useroption == 1:
            print("\nYour current balance is {}".format(userbalance[index]))
            b1.operationlist(index)

        elif useroption == 2:
            amount= int(input("\nEnter amount you want you want to withdraw : Rs"))
            b1.withdraw(amount,index)

        else:
            print("None of the above options selected. Please select any one of the provided options.")
            b1.operationlist(index)

    def withdraw(self, amt, index):
        self.amt= amt
        amount = amt
        self.index= index
        if amount > userbalance[index]:
            print("Oops! Insufficient funds.")
            b1.operationlist(index)

        rembalance = userbalance[index] - amount
        userbalance.remove(userbalance[index])
        userbalance.insert(index, rembalance)
        print("Your remaining balance is: ", userbalance[index])
        b1.operationlist(index)

b1 = Bank()
b1.start()

#code for VirtualDoctor
import xlrd
import pandas
from NewATM import Bank

path = "symptoms.xlsx"
book = xlrd.open_workbook(path)
sheet= book.sheet_by_index(0)
b2= Bank()

data= [[sheet.cell_value(r,c) for c in range (sheet.ncols)] for r in range (sheet.nrows)]

diseaselist= []
for i in range (1,sheet.nrows):
    diseaselist.append(sheet.cell_value(i,0))

symptoms=[]
for i in range (1, data.__len__()):
    tmp= data[i][1:5]
    symptoms.append(tmp)

print(symptoms)
inputlist = []
b2.start() #THIS DOES NOT WORKK !!!!!!!!!!!!!!!!!!!!! WHY ???

虚拟医生程序现在应该转到自动取款机,然后我可以继续我的代码,但这似乎不起作用


Tags: inselfforindexifdefpinrange
1条回答
网友
1楼 · 发布于 2024-10-02 02:36:26

您的问题是来自ATM类的代码需要在类本身中设置为全局变量的数据。就OOP而言,这是一种糟糕的做法,在您的特定情况下,代码无法工作,因为变量超出了main(虚拟医生)的作用域

要么将这些变量移到类本身(这仍然是一种糟糕的做法),要么将它们放在其他地方,在调用类时导入数据矩阵,并将其传递给每个单独的函数

最后,您将创建一个名为b1的bank实例以在类中使用,这在OOP中没有意义。将b1更改为self,以便调用self.function之类的函数(一个编辑是将b1.pinpasscheck()更改为self.pinpasscheck())

相关问题 更多 >

    热门问题