【问题标题】:Calling a function with a floating number调用带有浮点数的函数
【发布时间】:2017-05-21 11:03:01
【问题描述】:

您好,我正在尝试解决这个问题。我不断收到一条错误消息,说

不能将序列乘以“浮点”类型的非整数

我也在使用 tkinter 模块。

list=[]
print amount.get()     #Using the Tkinter Spinbox widget, .get()
                       #retrieves the amount/input in that widget
if amount.get() > 0:
    list.append(amount.get())
    amount = 0
    for a in list:             #Makes the number in a list a float no. 
        for b in a.split():
            try:
                amount += float(b)
            except:
                pass
    print amount
    function_one() * amount  #I'm trying to call the function 
                             #definition with the amount but I get the 
                             #error message         
    del list[0] #delete the amount for the user to input a different 
                #number

我试图用“数量”的次数调用函数(来自“if 语句”的数字“数量”:

def function_one():
   print "Select"

上面的定义中有更多的变量等。您不必创建 tkinter 小部件等,但是解决如何多次调用函数的解决方案会很棒!

如果你还是一头雾水,我很抱歉,例如,如果我的数量 = 5,我希望函数被调用 5 次。

谢谢你:)

【问题讨论】:

  • 是否可以按照您的语法?使用循环
  • 可能是另一种解决此问题的方法。也许是“for each”循环?
  • 您到底想做什么?设金额为 5.5,调用函数 5.5 次?

标签: python function user-interface tkinter floating-point


【解决方案1】:

你能不能不使用for循环,即:

for i in range(int(amount.get())): 
    function_one()

【讨论】:

  • 简短而简单。我的代码太长而且太复杂了。谢谢!它有效
猜你喜欢
  • 2015-07-31
  • 2021-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-26
相关资源
最近更新 更多