在执行数组循环Python代码时,它会给出错误TypeError

时间:2018-02-20 09:18:17

标签: python arrays typeerror

我一直在向Geeksforgeeks.org学习并尝试将此C代码写入python并运行它


  Traceback (most recent call last):   File
  "b:\Shall\Practice\Geeksforgeeks.org\recursiveblockswap.py", line 38,
  in <module> leftRotate(arr, 2, 7)

运行此代码时,它会显示一条错误,指出TypeError:&#39; int&#39;对象不可订阅

  

[运行] python   &#34; B:\难道\实践\ Geeksforgeeks.org \ recursiveblockswap.py&#34; File "b:\Shall\Practice\Geeksforgeeks.org\recursiveblockswap.py", line 32, in swap temp = arr[fi + i]

     

文件&#34; b:\ Shall \ Practice \ Geeksforgeeks.org \ recursiveblockswap.py&#34;,   第17行,在leftRotate leftRotate(arr,d,n-d)`

     

文件&#34; b:\ Shall \ Practice \ Geeksforgeeks.org \ recursiveblockswap.py&#34;,   第17行,在leftRotate leftRotate(arr,d,n-d)`

     

文件&#34; b:\ Shall \ Practice \ Geeksforgeeks.org \ recursiveblockswap.py&#34;,   第20行,在leftRotate leftRotate(arr [0] + n-d,2 * d-n,d)`

     

文件&#34; b:\ Shall \ Practice \ Geeksforgeeks.org \ recursiveblockswap.py&#34;,line   12,在leftRotate swap(arr,0,n-d,d)[BadImageFormatException: Could not load file or assembly 'file:///C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\385841ab\41679b95\App_global.asax.wuryexuy.dll' or one of its dependencies. An attempt was made to load a program with an incorrect format.] System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +0 System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +234 System.Reflection.Assembly.Load(AssemblyName assemblyRef, Evidence assemblySecurity) +64 System.CodeDom.Compiler.CompilerResults.get_CompiledAssembly() +116 System.Web.Compilation.BuildProvider.CreateBuildResult(CompilerResults results) +82 System.Web.Compilation.BuildProvider.GetBuildResult(CompilerResults results) +22 System.Web.Compilation.ApplicationBuildProvider.GetGlobalAsaxBuildResult(Boolean isPrecompiledApp) +12188385 System.Web.Compilation.BuildManager.CompileGlobalAsax() +52 System.Web.Compilation.BuildManager.EnsureTopLevelFilesCompiled() +335 [HttpException (0x80004005): Could not load file or assembly 'file:///C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\385841ab\41679b95\App_global.asax.wuryexuy.dll' or one of its dependencies. An attempt was made to load a program with an incorrect format.] System.Web.Compilation.BuildManager.ReportTopLevelCompilationException() +73 System.Web.Compilation.BuildManager.EnsureTopLevelFilesCompiled() +668 System.Web.Compilation.BuildManager.CallAppInitializeMethod() +39 System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException) +749 [HttpException (0x80004005): Could not load file or assembly 'file:///C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\385841ab\41679b95\App_global.asax.wuryexuy.dll' or one of its dependencies. An attempt was made to load a program with an incorrect format.] System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +579 System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +112 System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +716

     

TypeError:&#39; int&#39;对象不是可订阅的。

     

[完成]在0.209秒内退出代码= 1

1 个答案:

答案 0 :(得分:0)

leftRotate(arr[0]+n-d, 2*d-n, d)在此您将整数值传递给变量arr这导致temp = arr[fi + i]错误

TypeError: 'int' object is not subscriptable`

基本上意味着您正在访问int对象的索引。

旁注:

在python中你可以使用

交换值
arr[1], arr[2] = arr[2], arr[1]