我创建了两个程序,一个是ATM模拟器,另一个是虚拟医生,可以从excel表中读取数据,并根据用户输入来告知用户可能患有什么疾病。
现在我想将atm连接到虚拟医生,以便它从银行帐户中提取药品
我将atm导入到虚拟医生中,但是这些功能似乎无法正常工作,在调用时它们什么也没做,并且进程退出。
#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,然后我可以继续执行我的代码,但这似乎不起作用。
答案 0 :(得分:0)
您的问题是ATM类中的代码需要在类本身中设置为全局变量的数据。就OOP而言,这是一种不好的做法,在您的特定情况下,由于变量超出了主要(虚拟医生)的范围,因此代码无法正常工作。
要么将这些变量移到类本身中(这仍然是一种不好的做法),要么将它们保留在其他位置,并在调用类时导入数据矩阵,并将其传递给每个单独的函数。
最后,您要创建一个名为b1的bank实例以用于该类,就OOP而言,这没有任何意义。将b1更改为self,以便调用诸如self.function之类的函数(一项编辑将b1.pinpasscheck()更改为self.pinpasscheck())。