python - TypeError: bad operand type for unary -: 'str' -


i've got problem python 2.7.3-32bits on windows. put code see if can me out error. comments in spanish don't affect 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 == " " , len(arraypuntos) < 4):      print"por favor, introduzca los puntos de distorsión xc yc r e:"      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 run python code 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 unary -: 'str' 

the error message telling r string. can't negate string.

why string? well, seems come here:

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

the split method on string returns list of strings.

so, how solve this? well, if r is, say, string "22", float(r) float 22.0, , int(r) integer 22. 1 of want.

once add, say, r=int(r), --r no longer exception.


but isn't want. in python, --r means negation of negation of r—in other words, it's same -(-(r)), r. you're looking equivalent of c prefix operator--, decrements variable , returns new value. there no such operator in python; in fact, there no operators modify variable , return value.

this part of larger issue. python designed make statements , expressions distinct possible, avoid confusion. c designed make many things possible expressions, save typing. so, can't translate 1 other line line.

in case, have in 2 steps, thanasis petsas shows:

r -= 1 x = str(r) 

Comments

Popular posts from this blog

ios - iPhone/iPad different view orientations in different views , and apple approval process -

java Extracting Zip file -

C# WinForm - loading screen -