无法找出TypeError

时间:2018-11-08 16:39:21

标签: python function

我收到一个(TypeError:无法将序列乘以'float'类型的非整数)的错误,我不知道如何解决它。我已经搜索了该错误,但无法找出解决方法,因为有错误的代码示例与我遇到问题的代码不一样。

# Variable stuff
initialMagnitudes = input("What are the initial magnitudes of the 
vector? (separate with spaces)\t").split()
for string in initialMagnitudes:
    string = float(string)
initialDirections = input("What are the initial directions of the 
vector? (in degrees and separate with spaces)\t").split()
for string in initialDirections:
    string = float(string)
vector_i = []
vector_j = []
vectorEquation = "Xi + Yj"
resultantMagnitude = 0.0
resultantDirection = 0.0
resultantVectorEquation = "R = Xi + Yj"


def find_vector_equation():
    global vectorEquation
    global vector_i
    global vector_j
    for magnitude in initialMagnitudes:
        theta = 
float(initialDirections[initialMagnitudes.index(magnitude)])
        vector_i.append(math.cos(math.radians(theta))*magnitude)
        vector_j.append(math.sin(math.radians(theta))*magnitude)
        vectorEquation = "{}i + {}j".format(vector_i, vector_j)
    print("The resultant vector equation is: "+vectorEquation)
find_vector_equation()

这是所有可能成为问题一部分的代码。 预先感谢您提供的所有帮助!

1 个答案:

答案 0 :(得分:1)

这是问题所在

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Http;
using Windows.ApplicationModel.Background;
using GrovePi;
using GrovePi.I2CDevices;
using GrovePi.Sensors;
using GrovePi.Common;
using System.Threading.Tasks;
using Microsoft.Azure.Devices.Client;
using Newtonsoft.Json;

// The Background Application template is documented at http://go.microsoft.com/fwlink/?LinkID=533884&clcid=0x409

namespace GrooveTHS
{
    public sealed class StartupTask : IBackgroundTask
    {
        IRgbLcdDisplay LCDDisplay;
        public void Run(IBackgroundTaskInstance taskInstance)
        {
            IDHTTemperatureAndHumiditySensor sensor = DeviceFactory.Build.DHTTemperatureAndHumiditySensor(Pin.DigitalPin7, DHTModel.Dht11); // pinD7
            LCDDisplay = DeviceFactory.Build.RgbLcdDisplay(); // pinI2C-1
            IRotaryAngleSensor potentiometer = DeviceFactory.Build.RotaryAngleSensor(Pin.AnalogPin2); // pinA2
            ILed red = DeviceFactory.Build.Led(Pin.DigitalPin5); // pinD5
            ILed green = DeviceFactory.Build.Led(Pin.DigitalPin6); // pinD6
            IUltrasonicRangerSensor dsensor = DeviceFactory.Build.UltraSonicSensor(Pin.DigitalPin4); // pin D4

            double angle = 0;
            double tmax = 0;

            while (true)
            {
                Task.Delay(500).Wait();
                angle = potentiometer.SensorValue();
                sensor.Measure();
                string sensortemp = sensor.TemperatureInCelsius.ToString();
                tmax = Math.Floor(angle / 10);
                string sensorvalue = dsensor.MeasureInCentimeters().ToString();
                if (sensor.TemperatureInCelsius > tmax)
                {
                    System.Diagnostics.Debug.WriteLine("Raumtemperatur: " + sensortemp + "C " + "Tmax: " + tmax.ToString() + "C");

                    red.AnalogWrite(Convert.ToByte(240));
                    green.AnalogWrite(Convert.ToByte(0));
                }
                else
                {
                    System.Diagnostics.Debug.WriteLine("Raumtemperatur: " + sensortemp + "C" + "Tmax: " + tmax.ToString() + "C");

                    red.AnalogWrite(Convert.ToByte(0));
                    green.AnalogWrite(Convert.ToByte(240));
                }
                if (dsensor.MeasureInCentimeters() < 150)
                {
                    LCDDisplay.SetBacklightRgb(BitConverter.GetBytes(990)[0], BitConverter.GetBytes(990)[0], BitConverter.GetBytes(990)[0]);
                    LCDDisplay.SetText("Raumtemperatur: " + sensortemp + "C " + "Tmax: " + tmax.ToString() + "C");
                }
                else
                {
                    LCDDisplay.SetText("");
                    LCDDisplay.SetBacklightRgb(BitConverter.GetBytes(0)[0], BitConverter.GetBytes(0)[0], BitConverter.GetBytes(0)[0]);
                }
            }
        }


        static async void SendDeviceToCloudMessagesAsync(long inputDistance)
        {
            string iotHubUri = "IotHubAuburn.azure-devices.net"; // ! put in value !
            string deviceId = "jb"; // ! put in value !
            string deviceKey = "sHGJlQbLLMeMExNaqtvh8/7N7MHWlBZ0ESj2ePahSwQ="; // ! put in value !
            DateTime time = DateTime.UtcNow;

            var deviceClient = DeviceClient.Create(iotHubUri, AuthenticationMethodFactory.CreateAuthenticationWithRegistrySymmetricKey(deviceId, deviceKey), TransportType.Http1);

            JSON jsonStr = new JSON();
            jsonStr.distance = inputDistance;
            jsonStr.time = time;
            jsonStr.deviceId = deviceId;
            var list = JsonConvert.SerializeObject(jsonStr);
            System.Diagnostics.Debug.WriteLine(list);
            var message = new Message(Encoding.UTF8.GetBytes(list));

            await deviceClient.SendEventAsync(message);
        }
    }
}

这不会更改for string in initialMagnitudes: string = float(string) 中的任何内容。而是将变量initialMagnitudes更改为其字符串值的浮点版本,然后丢弃该浮点。 string语句中的循环变量是一个值,而不是列表中的插槽。它不知道它来自何处,分配它也不会将该值放回列表中。

由于for的项仍然是字符串,因此尝试乘以其中之一就是将字符串乘以浮点数,就会得到错误。

最好通过列表理解做到这一点:

initialMagnitudes