Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中与列表相乘_Python_Python 3.x - Fatal编程技术网

如何在python中与列表相乘

如何在python中与列表相乘,python,python-3.x,Python,Python 3.x,我是Python新手,我想将浮点值存储在列表中,然后使用列表中的值进行处理,直到我将其重置为空为止。我将抛出此错误: python TypeError: can't multiply sequence by non-int of type 'list' >>> 该程序是一个基本的距离、速度和时间计算器,我还没有添加任何类似单位的内容,但我已经跨过了这一关。如何在equale()函数中正确乘法 import math import random import os dista

我是Python新手,我想将浮点值存储在列表中,然后使用列表中的值进行处理,直到我将其重置为空为止。我将抛出此错误:

python TypeError: can't multiply sequence by non-int of type 'list' >>>
该程序是一个基本的距离、速度和时间计算器,我还没有添加任何类似单位的内容,但我已经跨过了这一关。如何在
equale()
函数中正确乘法

import math
import random
import os

distance = []
speed = []
time = []
result = []

def di():
    while True:
        try:
            di = float(input('Enter Distance:> '))
            distance.append(di)
            print('You entered', distance)
            sp()
        except ValueError:
            print('Thats not a number')
            pass

def sp():
    while True:
        try:
            sp = float(input('Enter Speed:> '))
            speed.append(sp)
            print('You have entered', speed)
            ti()
        except ValueError:
            print('Thats not a number')
            pass

def ti():
    while True:
        try:
            ti = float(input('Enter Time:> '))
            time.append(ti)
            print('You have entered', time)
            equate()
        except ValueError:
            print('Thats not a number')
            pass

def equate():
    print('What do you want to calculate?')
    option = input('1.Distance, \n2.Speed, \n3.Time, \n4.Exit Program, \n:>')
    if option == '1':
        res = speed * time
        result.append(res)
        print(result)
    elif option == '2':
        res = distance / time
        result.append(res)
        print(result)
    elif option == '3':
        res = distance / speed
        result.append(res)
        print(result)
    elif option == '4':
        sys.exit('Goodbye')
    else:
        print('Thats not an option')
        pass

def running():
    input('Distance Speed and Time Caluclator \nPress any key to enter the values')
    di()

running()
编辑:完全错误

win32上的Python 3.4.4(v3.4.4:737efcadf5a6,2015年12月20日,20:20:57)[MSC v.1600 64位(AMD64)] 有关详细信息,请键入“copyright”、“credits”或“license()”

=========================重新启动:C:\Python34\equations.py=====================
距离、速度和时间计算器
按任意键输入值
输入距离:>30
您输入了[30.0]
输入速度:>20
您已输入[20.0]
输入时间:>10
您已输入[10.0]
你想计算什么?
1.距离,
2.速度,
3.时间,
4.退出程序,
:>1
回溯(最近一次呼叫最后一次):
文件“C:\Python34\equations.py”,第69行,在
运行()
文件“C:\Python34\equations.py”,第67行,正在运行
di()
文件“C:\Python34\equations.py”,第16行,di格式
sp()
文件“C:\Python34\equations.py”,第27行,在sp中
ti()
文件“C:\Python34\equations.py”,第38行,在ti中
等同于
文件“C:\Python34\equations.py”,第47行,等号
res=速度*时间
TypeError:无法将序列与“list”类型的非int相乘

当您在
equale
函数中进行乘法时,似乎发生了错误。我认为你是在试图用一个列表乘以一个列表,正如@TigerhawkT3所说的那样。相反,我建议在
equale()
函数中引用时使用
速度[-1]
时间[-1]
距离[-1]
(以获取用户刚刚输入的最后一项)。例如,对于乘法:

if option == '1':
    res = speed[-1] * time[-1]
    result.append(res)
    print(result)

编辑:感谢您使问题更加简洁

首先,您不需要
,而
在每个函数上循环一次就足够了。 其次,要计算列表中每个项目的值。在您的情况下,选项示例:

if option == '1':
        #it would be nice to check if arrays are the same size etc.
        for i in range(len(time)):
            result.append(speed[i] * time[i])

        print(result)

请记住,结果是一个arrray。

请在您的问题中包含完整的错误。
速度
时间
都是列表,您要将它们相乘。你不能那样做。用Error编辑的帖子这个工作很好,也做了我需要的,非常感谢!我很感激快速解决,这似乎是一个很好的方法。你能解释一下这方面的情况吗?这是否意味着它将选择列表中存储的每个数字并使用它?或者它会在列表中找到一个特定的部分?很抱歉听起来像这样一个傻瓜,这对我来说是相当新鲜的
,因为我在范围内(len(time))
是一个相当。。。非肾盂途径,通常不鼓励
对于spd,邮政编码的tm(速度、时间)
可能更好。结果也是一个列表。:-)我只使用len(time)来获取数组的长度;)。在我的例子中,len(time)=整数中的项目数。在这个循环中,它只是一个索引,从0到len(时间),步骤1,比如0,1,2,…,max。然后在循环中,我通过带括号的索引获取每个项:数组[i]返回带索引的项,例如时间[0],获取第一个元素。
if option == '1':
        #it would be nice to check if arrays are the same size etc.
        for i in range(len(time)):
            result.append(speed[i] * time[i])

        print(result)