我正在建立一个计算器,该计算器可以接收变量的各种统计分布并将它们相乘。我在从输入框中返回值以进行计算时遇到问题。所有需要输入到计算器中的6个数字都是浮点变量。
我已略缩略并更改了代码,以使其更易于理解错误。
我的思考过程是这样。
步骤4是我遇到问题的地方。我收到一个类型错误,指出“'浮动对象'不能解释为整数”。我不知道Python在哪里尝试在此脚本中解释整数。我以为我已经将所有变量都转换为浮点数了。
from tkinter import *
import numpy as np
window = Tk()
window.geometry('1100x700')
window.title('Discrete Gas Calculator')
def example():
#area variables
dalv = float(alv.get())
dabv = float(abv.get())
dahv = float(ahv.get())
dalp = float(alp.get())
dabp = float(abp.get())
dahp = float(ahp.get())
dnumbint = float(numbint.get())
problemex= np.random.choice([dalv,dabv,dahv], dnumbint, p=[dalp,dabp,dahp] )
print(problemex)
#Number of iterations label
numbitlabel = Label(text = 'Number of Iterations', fg = 'black', bg = 'white', width = 17 ).grid(row = 9, column = 0)
#y labels
arealabel = Label(text = 'Area', fg = 'black', bg = 'white', width = 17 ).grid(row = 1, column = 0)
#x labels
unitslabel = Label(text = 'Units', fg = 'black', bg = 'white', width = 17 ).grid(row = 0, column = 1)
lvlabel = Label(text = 'Low Value', fg = 'black', bg = 'white', width = 17 ).grid(row = 0, column = 2)
bvlabel = Label(text = 'Base Value', fg = 'black', bg = 'white', width = 17 ).grid(row = 0, column = 3)
hvlabel = Label(text = 'High Value', fg = 'black', bg = 'white', width = 17 ).grid(row = 0, column = 4)
lplabel = Label(text = 'Low Probability', fg = 'black', bg = 'white', width = 17 ).grid(row = 0, column = 5)
bplabel = Label(text = 'Base Probability', fg = 'black', bg = 'white', width = 17 ).grid(row = 0, column = 6)
hplabel = Label(text = 'High Probability', fg = 'black', bg = 'white', width = 17 ).grid(row = 0, column = 7)
calcbutton1 = Button(text='Calculate Volumes', width=15, height = 2,fg = 'black', bg = 'white', command = example).place(x = 200, y = 250)
#initially define variables that will be called in entry box
alv = StringVar()
abv = StringVar()
ahv = StringVar()
alp = StringVar()
abp = StringVar()
ahp = StringVar()
#number of iterations variable
numbint = StringVar()
#this section contains the entry box creation for each variable
#area entry boxes
alventry = Entry(textvariable = alv).grid(row = 1, column = 2)
abventry = Entry(textvariable = abv).grid(row = 1, column = 3)
ahventry = Entry(textvariable = ahv).grid(row = 1, column = 4)
alpventry = Entry(textvariable = alp).grid(row = 1, column = 5)
abplventry = Entry(textvariable = abp).grid(row = 1, column = 6)
ahpventry = Entry(textvariable = ahp).grid(row = 1, column = 7)
#number of iterations entry box
numbintentry = Entry(textvariable = numbint).grid(row = 9, column = 1)
window.mainloop()
请让我知道我是否可以澄清任何事情,我会尽力而为!
感谢阅读。
答案 0 :(得分:0)
问题出在下面的这一行:
problemex= np.random.choice([dalv,dabv,dahv], dnumbint, p=[dalp,dabp,dahp] )
如果看到回溯错误并阅读numpy文档,则该错误非常明显。
在这种情况下,size
参数dumbint
必须是整数,但是您已经传递了float
。
因此,将其键入int
中就可以了。
实际上,用下面的行替换可以使程序正常工作:
problemex= np.random.choice([dalv,dabv,dahv], int(dnumbint), p=[dalp,dabp,dahp] )