TypeError: invalid operand type for unary -: 'str'

I have a problem with Python 2.7.3-32bits on Windows. I put this code to find out if anyone can help me with this error. Comments are written in Spanish, but this does not affect the code.

import gtk import numpy import math import os #Pedimos el nombre de la imagen de origen nombreFich = input("Por favor, introduzca el nombre de la imagen de origen:") #Validar que existe el fichero imagen1 = gtk.Image() imagen1.set_from_file('C:\\Users\\xxx\\Desktop\\xxxx.png') pb1 = imagen1.get_pixbuf() pm1 = pb1.get_pixels_array() #Hacemos una copia de la imagen pm2 = pm1.copy() #Validamos los puntos de distorsion hasta que sean validos puntos = " " arrayPuntos = " " while(puntos == " " and len(arrayPuntos) < 4): print"Por favor, introduzca los puntos de distorsión xc yc re:" puntos= raw_input() arrayPuntos = puntos.split(" ") #Sacamos los puntos separando la cadena por el caracter espacio xc =(puntos[0]) yc =(puntos[1]) r =(puntos[2]) e =(puntos[3]) #función que calcula el grado de distorsión def grado(self,z,e): if(z>1): return 1 elif(e<0): return (1/z)**(-e/(1-e)) else: return z**e #Distorsionamos la imagen def distors(xc,yc,r,e,x,y): d = math.sqrt(x**2+y**2)#Sacamos la distancia z = d/r if(z!=0): g=grado(z,e) xm=x*g ym=y*g return xm,ym else: xm=x ym=y return xm,ym def recorrido (pm1, xc, yc, r, e): pm2 = pm1.copy() x= str(--r) y= str(--r) while (y <= r): while (x <= r): xm, ym = mover(xc, yc, r, e, x, y) pm2[yc+y][xc+x] = pm1[yc+ym][xc+xm] x = x+1 y= y+1 x= -r return pm2 pm2 = recorrido(pm1, xc, yc, r, e) #Guardamos los cambios pb2 = gtk.gdk.pixbuf_new_from_array(pm2,pb1.get_colorspace(),pb1.get_bits_per_sample()) nomfich2 = nombreFich+"_copia" ext = os.path.splitext("C:\\Users\xxx\Desktop\xxxx.png_copia")[1][1:].lower() pb2.save("C:\\Users\xxx\Desktop\xxxx.png_copia",ext) print"FINISH" 

When I run the python code, I get the following error:

  Traceback (most recent call last): File "F:\Dropbox\Práctica Pitón\Práctica3.0.py", line 71, in <module> pm2 = recorrido(pm1, xc, yc, r, e) File "F:\Dropbox\Práctica Pitón\Práctica3.0.py", line 59, in recorrido x= str(--r) TypeError: bad operand type for unary -: 'str' 
+4
source share
2 answers

The error message reports that r is a string. You cannot cancel a row.

Why is this a string? Well, it seems to come from here:

 # ... puntos= raw_input() arrayPuntos = puntos.split(" ") # ... r =(puntos[2]) 

The split method in a string returns a list of strings.

So how do you solve this? Well, if r is, say, the string "22", then float(r) is float 22.0 , and int(r) is the integer 22 . One of them is probably what you want.

Once you add, say r=int(r) , your --r will no longer be an exception.


But that is probably not what you want. In Python, --r simply means negating the negation of r - in other words, it's the same as -(-(r)) , which is just r . You are probably looking for the equivalent of the C -- prefix operator, which reduces this variable and returns a new value. There is no such operator in Python; in fact, there are no statements that modify a variable and then return a value.

This is part of a larger problem. Python is designed to make expressions and expressions as distinct as possible to avoid confusion. C is designed to create as many expressions as possible in order to preserve text input. Thus, you often can’t just translate one into another line by line.

In this case, you must do this in two steps, as Thanasis Petsas shows:

 r -= 1 x = str(r) 
+4
source

Increments ++ and decrements -- not supported in python. You can use instead:

 r -= 1 x = str(r) 
+2
source

All Articles