Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.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 类型错误:一元操作数-:';str';_Python_String_Python 2.7_Typeerror - Fatal编程技术网

Python 类型错误:一元操作数-:';str';

Python 类型错误:一元操作数-:';str';,python,string,python-2.7,typeerror,Python,String,Python 2.7,Typeerror,我在Windows上遇到了Python 2.7.3-32位的问题。我写下这段代码是为了看看是否有人能帮我解决这个错误。注释是西班牙语的,但不会影响代码 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:")

我在Windows上遇到了Python 2.7.3-32位的问题。我写下这段代码是为了看看是否有人能帮我解决这个错误。注释是西班牙语的,但不会影响代码

 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 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"
导入gtk
进口numpy
输入数学
导入操作系统
#原始图像的名称
nombreFich=输入(“赞成,介绍origen图像名称:”)
#艾尔·费希罗的存在
imagen1=gtk.Image()
imagen1.从文件('C:\\Users\\xxx\\Desktop\\xxxx.png')设置
pb1=imagen1.get_pixbuf()
pm1=pb1.get_像素_数组()
#你的照片是什么
pm2=pm1.copy()
#Validamos los puntos de Distorision hasta que sean validos
puntos=“”
ArrayPantos=“”
而(puntos==”和len(arraypontos)<4):
打印“支持,介绍”字样:
puntos=原始输入()
ArrayPantos=puntos.split(“”)
#埃斯帕西奥城堡
xc=(puntos[0])
yc=(puntos[1])
r=(puntos[2])
e=(puntos[3])
#分布式计算的功能
def梯度(自、z、e):
如果(z>1):
返回1

python中不支持elif(e增量
++
和减量
--
运算符。 您可以改为使用以下选项:

r -= 1
x = str(r)
r -= 1
x = str(r)

错误消息告诉您
r
是一个字符串。您不能对字符串求反

为什么它是一根线?嗯,它似乎来自这里:

# ...
puntos= raw_input()
arrayPuntos = puntos.split(" ")
# ...
r =(puntos[2])
字符串上的
split
方法返回字符串列表

那么,如何解决这个问题呢?如果
r
是字符串“22”,那么
float(r)
就是float
22.0
,而
int(r)
就是整数
22
。其中一个可能就是您想要的

一旦你添加了,比如说,
r=int(r)
,你的
--r
将不再是一个例外


但这可能不是您想要的。在Python中,
--r
只是表示对
r
的否定的否定,换句话说,它与
-((r))相同
,它只是
r
。您可能正在寻找C前缀运算符的等价物
--
,该运算符递减变量并返回新值。Python中没有此类运算符;事实上,没有任何运算符修改变量然后返回值

这是一个更大问题的一部分。Python的设计目的是使语句和表达式尽可能不同,以避免混淆。C的设计目的是使尽可能多的东西成为表达式,以节省键入。因此,通常不能一行一行地将一个翻译成另一个

在这种情况下,您必须分两步进行,如Thanasis Petsas所示:

r -= 1
x = str(r)