2017-10-14 90 views
-1

我有以下代碼:Python3:被0除錯誤帶有小數

from tkinter import * 

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

global BTC 
BTC = 0.00

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() 

上面的代碼是一個小程序的IM工作關於比特幣,它不是成品。

在管線88我有這樣的代碼: BTC = BTC +(INT(text_E)%INT(BTCPrice))

我由0錯誤得到司... text_E是從未0 BTCPrice被設定到0.01。 如果我改變這樣的東西像9或6578我不會得到錯誤。所以即時猜測,它需要十進制數(並不只是0.01; 0.88和0.02828太)作爲0.我嘗試使用/和%分開,但我得到了相同的結果。有人能告訴我如何解決這個問題嗎? 謝謝

+0

你知道'int'的含義嗎? – user2357112

+0

您正在將數字從'double'轉換爲'int',因此所有小數都轉換爲int(四捨五入)。因此所有值都以'0'開始轉換爲'0'。只需使用像'(text_E%BTCPrice)'這樣的語句' –

+0

這對我們來說太多瞭解代碼。請把它凝聚成一個[mcve] –

回答

0

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 %=