【问题标题】:Can't multiply sequence by non-int of type 'float'? [duplicate]不能将序列乘以“浮点”类型的非整数? [复制]
【发布时间】:2017-03-07 02:50:21
【问题描述】:

我正在尝试为课堂上的一个项目编写一个计算器,但总是遇到错误:

Traceback(最近一次调用最后一次): 文件“C:\Users\shane\PythonPrograms\louie.py”,第 56 行,在 tax_percent = (价格 * 税) TypeError:不能将序列乘以“浮点”类型的非整数

我的代码如下。谁能帮我调试一下?

size_types = {'mini',
              'regular',
              'large'
}

meat_types = {'pork',
              'beef',
              'chicken'
}

def price_pork_meal(size):
    if size == 'mini':
        return 3.00
    if size == 'regurlar':
        return 4.00
    if size == 'large':
        return 6.00
    else:
        return (input('Please try again:'))
    return size

def price_beef_meal(size):
    if size == 'mini':
        return 4.00
    if size == 'regular':
        return 7.00
    if size == 'large':
        return 9.00
    else:
        return (input('Please try again:'))
    return size

def price_chicken_meal(size):
    if size == 'mini':
        return 3.50
    if size == 'regular':
        return 6.00
    if size == 'large':
        return 8.00
    else:
        return (input('Please try again:'))
    return size

def final_price(price, tax_percent):
    price = size
    total_price = price + tax_percent
    return total_price


size = (input('Please enter mini, regular, or large:\n'))

price = size

tax = 0.825

tax_percent = (price * tax)

print(final_price(price, tax_percent))

【问题讨论】:

  • 请在您的问题中填写完整代码
  • 似乎变量pricelisttuple,而您正试图将其乘以float

标签: python python-3.x


【解决方案1】:

您的方法在 python 中无效。如错误中所述,您不能将序列与浮点值相乘。

这行不通:

a = (1,2,3)
b = a * 0.25

你可以使用numpy,它支持这个(以及更多),或者你自己实现这个:

a = (1,2,3)
b = list(map(lambda x: x*0.2, a))  # or many other approaches; outer list needed in python3 to make it a list instead of an generator-expression

【讨论】:

    猜你喜欢
    • 2011-04-06
    • 2012-09-17
    • 2018-12-19
    • 1970-01-01
    • 2013-09-11
    • 2015-06-27
    • 1970-01-01
    相关资源
    最近更新 更多