标签云

微信群

扫码加入我们

WeChat QR Code

I'm new to Python and have been learning only for a week so please pardon me if the answer seems really obvious to those of you who are experienced. I have written a simple program as follows and don't understand why I don't get "error: list index out of range".def sum13(nums):sum=0for i in range(1,len(nums)):if nums[i]==13 or nums[i-1]==13:sum=sumelse:sum=sum+nums[i]if len(nums)>=1:if nums[0]==13:sum=sumelse:sum=sum+nums[0]return sumprint(sum13([]))When using the for loop in the function sum13(nums), I indicated range(1,len(nums)), and index 1 doesn't exist in an empty list. Why is there no error: list index out of range when I use the function sum13(nums) on an empty list?