簡體   English   中英

此 Python 代碼適用於在線編譯器,但不適用於 spyder

[英]This Python code works in online compiler but not in spyder

此代碼適用於在線 Python 編譯器,但不適用於 Spyder(我只能在考試中使用 Spyder)。 少數情況下不起作用(例如在打印“資金不足”后使用方法等待我輸入長輸入)

這是問題:

設計一個名為 Account 的類,它包含: 一個名為 accountno 的私有 int 數據字段,用於帳戶。 賬戶的一個名為 balance 的私有浮動數據字段。 一個構造函數,它使用指定的 accountno 和 Initial balance (默認 100) 創建一個帳戶。 一個名為withdraws 的方法從賬戶中提取指定的金額。 每個帳戶應保持至少 100 的余額。 一種名為 deposit 的方法,將指定金額存入特定帳戶。 編寫一個程序,維護“n”個具有唯一 accountno 的 Account 對象並支持以下操作。 a) 新賬戶創建 b) 給定賬戶的存款操作 c) 給定賬戶的提款操作 d) 顯示余額最高的賬戶

這是我寫的代碼:

class bank ():
    def __init__(self,a):
        self.__balance=100
        self.__accno=a 
    def depo (self,d):
        self.__balance+=d
        print("current balnce ",self.__balance)
    def withd(self,w):
        if w>=self.__balance:
            print("inssuficinet fund ")
        else :
            self.__balance-=w
            print("current balnce ",self.__balance)
    def getbal(self):
        return self.__balance
    def getacc (self):
        return self.__accno 
    
l1=[]#accno
l2=[]#obj 
l3=[]#max 

f=True 
while(f):
    c=int(input("1.create 2.deposit 3.withdraw 4.display max "))
    if c==1:
        a=int(input("enter the account number "))
        if a in l1:
            print("account ni already present  ")
        else:
            l1.append(a)
            l2.append(bank(a))
            
    if c==2:
        try:
            a=int(input("enter the account number "))
            i=l1.index(a)
            d=int(input("enter the amount to be deposited "))
            l2[i].depo(d)
        except:
            print("not found")
    if c==3:
        try:
            a=int(input("enter the account number "))
            i=l1.index(a)
            w=int(input("enter the amount to be withdrawed "))
            l2[i].withd(w)
        except:
            print("not found")
        
            
    
    if c==4:
        if len(l1)==0:
            print("no account found ")
        else :
            for i in l2:
                l3.append(i.getbal())
            j=l3.index(max(l3))
            print("acc with max bal is ",l2[j].getacc())
            l3=[]
    if c==5:
        f=False

嗨,親愛的@Raksha Kamath ,我測試了您的代碼,看起來一切正常,問題可能出在您的Spider IDE上。

gedflod@localhost:~/home$ python3 m.py

輸出

1.create 2.deposit 3.withdraw 4.display max 1
enter the account number 45
1.create 2.deposit 3.withdraw 4.display max 7
1.create 2.deposit 3.withdraw 4.display max 9
1.create 2.deposit 3.withdraw 4.display max 3
enter the account number 56
not found
1.create 2.deposit 3.withdraw 4.display max 

我會建議您嘗試不同的 IDE(例如PyCharm 、 PyIDLE 等...)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM