从模块导入 class 但功能不工作
Importing class from module but functions not working
我创建了两个程序,一个是 ATM 模拟器,另一个是 Virtual Doctor,它从 excel sheet 读取数据并根据用户输入告诉用户可能患有什么疾病患有.
现在我想将 atm 连接到虚拟医生,以便它从银行账户中提取药品数量
我将 atm 导入了 virtual doctor,但功能似乎不起作用,调用时它们什么也不做,进程退出。
#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 ???
虚拟医生程序现在应该转到 atm,然后我可以继续我的代码,但这似乎不起作用。
您的问题是来自您的 ATM class 的代码需要在 class 本身中设置为全局变量的数据。就 OOP 而言,这是不好的做法,在您的特定情况下,代码将无法正常工作,因为变量超出了主要(虚拟医生)的范围。
要么将这些变量移动到 class 本身(这仍然是不好的做法),要么将它们保留在其他地方并在调用 class 时导入数据矩阵,并将其传递给每个单独的函数.
最后,您要创建一个名为 b1 的银行实例以在 class 中使用,这在 OOP 方面没有意义。将 b1 更改为 self,这样您就可以调用 self.function 之类的函数(一次编辑将 b1.pinpasscheck() 更改为 self.pinpasscheck())。
我创建了两个程序,一个是 ATM 模拟器,另一个是 Virtual Doctor,它从 excel sheet 读取数据并根据用户输入告诉用户可能患有什么疾病患有.
现在我想将 atm 连接到虚拟医生,以便它从银行账户中提取药品数量
我将 atm 导入了 virtual doctor,但功能似乎不起作用,调用时它们什么也不做,进程退出。
#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 ???
虚拟医生程序现在应该转到 atm,然后我可以继续我的代码,但这似乎不起作用。
您的问题是来自您的 ATM class 的代码需要在 class 本身中设置为全局变量的数据。就 OOP 而言,这是不好的做法,在您的特定情况下,代码将无法正常工作,因为变量超出了主要(虚拟医生)的范围。
要么将这些变量移动到 class 本身(这仍然是不好的做法),要么将它们保留在其他地方并在调用 class 时导入数据矩阵,并将其传递给每个单独的函数.
最后,您要创建一个名为 b1 的银行实例以在 class 中使用,这在 OOP 方面没有意义。将 b1 更改为 self,这样您就可以调用 self.function 之类的函数(一次编辑将 b1.pinpasscheck() 更改为 self.pinpasscheck())。