【问题标题】:Python3: Division by 0 error with decimalsPython3:用小数除以 0 错误
【发布时间】:2017-10-14 18:57:27
【问题描述】:

我有以下代码:

    from tkinter import *

################### Variables
Screenx = "640"
Screeny = "480"
screenSize = str(Screenx + "x" + Screeny)

global BTC
BTC = 0.000123

global processor
processor = 5

global USD
USD = 150000

global Auto_Miners
Auto_Miners = 1

global Computers
Computers = 3

global MinePower
MinePower = Computers * (0.0001 * processor)

global entryText
entryText = ('empty')

global entryPurpose
entryPurpose = "Nothing"

global BTCPrice
BTCPrice = 0.5



################## Function Definitions

def Mine_BTC():
    global MinePower
    global BTC
    BTC = BTC + MinePower
    print("Mining " + str(MinePower) + " BitCoins...")
    BitCoinLabel2.config(text=str(BTC))

def Buy_AM():
    global Auto_Miners
    global USD
    if USD >= 100:
        Auto_Miners = Auto_Miners + 1
        print("Bought 1 autominer...")
        AutominersLabel1.config(text=Auto_Miners)
        USD = USD - 100
    else:
        print("Damn you are poor...")

def Buy_Computers():
    global Computers
    global USD
    if USD >= 300:
        Computers = Computers + 1
        print("Bought 1 Computer...")
        ComputerLabel1.config(text=Computers)
        USD = USD - 300
    else:
        print("Damn you are poor...")

def Buy_Processor():
    global USD
    global processor
    if USD >= 450:
        processor = processor + 1
        print("PP + 1")
        ProLabel.config(text=processor)
        USD = USD - 450
    else:
        print("What a poor man...")


def EnterButton():
    global entryText
    global entryPurpose
    global BTC
    global USD
    text_E = EntryA.get()
    print(text_E)
    if entryPurpose == ("How many USD to BTC?"):
        BTC = BTC + (int(text_E) % int(BTCPrice))
        print(BTC)
        USD = USD - text_E
        print(USD)
    elif entryPurpose == ("How many BTC to USD?"):
        USD = text_E * BTCPrice
        print(USD)
        BTC = BTC - int(text_E)
        print(BTC)










def BuyBTC():
    global entryPurpose
    print(entryPurpose)
    entryPurpose = ("How many USD to BTC?")
    print(entryPurpose)
    entryP.config(text=entryPurpose)

def SellBTC():
    entryPurpose = "How many BTC to USD?"
    entryP.config(text=entryPurpose)





################# Screen Mainloop

screen = Tk()
screen.title("BitCoin Simulator")
screen.geometry(screenSize)

canvas = Canvas()
canvas.pack()
canvas.create_line(10, 0, 10, 600)
canvas.create_rectangle(200, 30, 350, 200)



EntryA = Entry(screen, textvariable=entryText)
EntryA.place(x=150, y=300)

EntryB = Button(screen, text="Enter", bg="blue")
EntryB.config(command=EnterButton)
EntryB.place(x=170, y=350)


BitCoinLabel1 = Label(screen, text="BitCoins: ").place(x=150, y=10)
BitCoinLabel2 = Label(screen, text=str(BTC)) ###
BitCoinLabel2.place(x=220, y=10)

MineButton = Button(screen, text="Mine")
MineButton.config(command=Mine_BTC)
MineButton.place(x=150, y=30)

AutominersButton = Button(screen, text="Auto-Miners:")
AutominersButton.config(command=Buy_AM)
AutominersButton.place(x=150, y=80)

AutominersLabel1 = Label(screen, text=Auto_Miners)
AutominersLabel1.place(x=270, y=85)

ComputersButton = Button(screen, text="Computers:")
ComputersButton.config(command=Buy_Computers)
ComputersButton.place(x=150, y=120)

ComputerLabel1 = Label(screen, text=Computers)
ComputerLabel1.place(x=270, y=120)

ProButton = Button(screen, text="Processors:")
ProButton.config(command=Buy_Processor)
ProButton.place(x=150, y=180)

ProLabel = Label(screen, text=processor)
ProLabel.place(x=285, y=180)

BuyButton = Button(screen, text="BUY")
BuyButton.config(command=BuyBTC)
BuyButton.place(x=340, y=170)

SellButton = Button(screen, text="SELL")
SellButton.config(command=SellBTC)
SellButton.place(x=410, y=170)

entryP = Label(screen, text=str(entryPurpose))
entryP.place(x=150, y=270)




screen.mainloop()

上面的代码是我正在做的一个关于比特币的小程序,它还没有完成。

在第 88 行,我有以下代码: BTC = BTC + (int(text_E) % int(BTCCrice))

我得到一个除以 0 的错误... text_E 从不为 0。BTCPrice 设置为 0.01。 如果我将其更改为 9 或 6578 之类的内容,我不会收到错误消息。所以我猜测它将十进制数(不仅是 0.01;0.88 和 0.02828)作为 0。我尝试使用 / 和 % 进行除法,但我得到了相同的结果。有人可以告诉我如何解决这个问题吗? 谢谢

【问题讨论】:

  • 你知道int是什么意思吗?
  • 您正在将数字从double 转换为int,因此所有十进制数都转换为int(四舍五入)。因此,所有以0. 开头的值都转换为0。只需使用(text_E % BTCPrice) 之类的语句
  • 代码太多,我们无法理解。请将其压缩为minimal reproducible example

标签: python-3.x decimal divide-by-zero


【解决方案1】:

int 函数始终将输入向下舍入为最接近的整数。 int(0.01) 计算结果为 0,因为小于 0.01 的最大整数是 0。我个人不明白您为什么要将 BTCPrice 变成整数,但是,嘿,我只是来回答您的问题。

不过还有一些其他的事情:

如果在全局级别声明变量,则无需说global variableglobal 仅在从不同范围内修改变量时使用。

不用写BTC = BTC + (int(text_E) % int(BTCPrice)),你可以写BTC += (int(text_E) % int(BTCPrice))。这是一个复合语句。通常,x += y 仅表示 x = x + y。这也适用于-=, *=, /=, **= (exponentiation), and %=

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多