【问题标题】:ValueError: invalid literal for int() with base 10: ''. Please help me hereValueError: int() 以 10 为底的无效文字:''。请在这里帮助我
【发布时间】:2021-06-22 08:17:31
【问题描述】:

问题出现在这行代码 newAmountForAcc1 = int(currBalance) - int(amountToBeSent),我只想将金额从一个银行账户转移到另一个银行账户,我有一个输入框(我使用Tkinter),以获取我使用 .get() 函数的数量。当我运行代码时,它显示 ValueError: invalid literal for int() with base 10: ''。这是函数

def transferMoney():
        newTransactionWindow = Tk()
        newTransactionWindow.title("Money Transfer")
        global secAcc
        secAcc=Entry(newTransactionWindow)
        secAcc.grid(row=0, column=1, padx=30)
        global amount
        amount=Entry(newTransactionWindow)
        amount.grid(row=1, column=1, padx=30)
    
        secAccLabel = Label(newTransactionWindow, text="Enter Receiver's Account number:")
        secAccLabel.grid(row=0, column=0, padx=30)
        amountLabel = Label(newTransactionWindow, text="Enter the amount to send:")
        amountLabel.grid(row=1, column=0, padx=30)
    
        amountToBeSent = amount.get()
    
        acc1 = deleteBox.get()
        acc2 = secAcc.get()
    
        statement1=f"select * from bank where ACC_NUM={acc1}"
        mycursor.execute(statement1)
        record = mycursor.fetchall()
        currBalance = 0
        for x in record:
            currBalance = x[7]
    
        newAmountForAcc1 = int(currBalance) - int(amountToBeSent)
        statement2=f"update bank set BALANCE = {newAmountForAcc1} where ACC_NUM={acc1}"
        mycursor.execute(statement2)
    
        newAmountForAcc2 = currBalance + amountToBeSent
        statement4 = f"update bank set BALANCE = {newAmountForAcc2} where ACC_NUM={acc2}"
        mycursor.execute(statement4)
        sendMoneyButton = Button(newTransactionWindow, text="Send Money", command=send)
        sendMoneyButton.grid(row=2, column=0, columnspan=2, pady=10, ipadx=50)
        mydb.commit()
        transactionCompletedWindow = Tk()
        transactionCompletedWindow.title("Transaction successful")
        transactionCompletedWindowLabel = Label(transactionCompletedWindow, text="Transaction completed successfully")

【问题讨论】:

  • 您的值之一是空字符串,不能转换为 int。追溯该值的来源!

标签: python transactions valueerror tkinter-entry


【解决方案1】:

也许其中一个变量包含一个不能表示为数字的符号。例如:

int('15a') # error
int('1.5') # error
int('1,5') # error
int('15') # nice

如果变量字面量应该是小数,则使用float():

float('1.5') # nice
float('15') # 15.0
# but
float('1,5') # error!
float('1,5'.replace(',', '.')) # fixes it

【讨论】:

    猜你喜欢
    • 2018-09-09
    • 2020-01-04
    • 2010-12-22
    • 2011-07-07
    • 2019-10-14
    • 2022-05-19
    相关资源
    最近更新 更多