Error: TypeError: can not multiply sequence by non-int of type 'str'

0

What is the following error?:

  

TypeError: can not multiply sequence by non-int of type 'str'

This is my code:

import math

print("teoria de inventarios")
print("elige el modelo")
print("1.-modelo deterministico")
print("2.-modelo con rebastecimiento instantaneo en un periodo de tiempo ")
print("3.-modelo de lote economico de producion")
print("4.-modelo probabilistico")
while (True):
    opcion=input(">")

    if (opcion=="1"):
        Q=0
        print("ingrese K")
        K=input()
        print("ingrese D")
        D=input()
        #print("ingrese T")
        #T=input()
        #print("ingrese P")
        #P=input()
        print("ingrese H")
        H=input()
        Q=2*K*D/H**1/2
        print(Q)

    elif(opcion=="2"):
        Q=0      
        print("ingrese K")
        K=input()
        print("ingrese D")
        D=input()
        #print("ingrese T")
        #T=input()
        print("ingrese P")
        P=input()
        print("ingrese H")
        H=input()
        Q=2*K*D/H
        raiz=math.sqrt(Q)
        print(Q)

    elif(opcion=="3"):
        print(" opcion 3")
    elif(opcion=="4"):
        print(" opcion 4")
    else:
        print("opcion invalida")
    
asked by Francisco Aczayacatl Garcia Go 07.12.2017 в 02:21
source

1 answer

0

the input function returns a string, for example you can check it by printing the type:

valor = input()
print(type(valor))

This returns the following:

<class 'str'>

The simple solution is to convert the string to float or int, that is, replace:

valor = input()

by:

valor = float(input())

Optional:

In addition to this I gave myself the time to compress your code so that it can be easily configured as shown below:

import math

print("teoria de inventarios")
print("elige el modelo")
print("1.-modelo deterministico")
print("2.-modelo con rebastecimiento instantaneo en un periodo de tiempo ")
print("3.-modelo de lote economico de producion")
print("4.-modelo probabilistico")


data = {"1": (["K", "D", "H"], lambda K, D, H:  2*K*D/H**1/2 ),
         "2": (["K", "D", "P", "H"], lambda K, D, P, H: math.sqrt(2*K*D/H))}

while True:
    opcion = input(">")
    if opcion in data.keys():
        variables, fun = data[opcion]
        args = []
        for variable in variables:
            print("ingrese {}".format(variable))
            valor = float(input())
            args.append(valor)
        resultado = fun(*args)
        print(resultado)
    else:
        print("opcion invalida")

Use:

If you want to add more options you must follow the following steps:

  • Add option.
  • A list of the names of the variables that should be requested.
  • a lambda function indicating the formula.
  • For example:

    Let's say we want to use the following formula associated with the "3" option:

    a**2 + b*p
    

    Then you must have the following:

    • option: "3"
    • list of variables: ["a", "b", "p"]
    • function: lambda a, b, p: a**2 + b*p

    Getting the following:

    data = {"1": (["K", "D", "H"], lambda K, D, H:  2*K*D/H**1/2 ),
             "2": (["K", "D", "P", "H"], lambda K, D, P, H: math.sqrt(2*K*D/H)),
             "3": (["a", "b", "p"], lambda a, b, p:  a**2 + b*p)}
    
        
    answered by 07.12.2017 / 05:16
    source