list index out of range with if condition [closed] - python

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
num_array = list()
num = int(raw_input("Enter how many elements you want:"))
print 'Enter numbers in array: '
for i in range(int(num)):
n = raw_input("num :")
num_array.append(int(n))
print 'ARRAY: ',num_array
b = 0
count = 0
while b< num -1 or b>0:
count = count+1
if num_array[b]!= 0:
b = b + num_array[b]
else:
b = (b + num_array[b])*2
print count
i am trying to get an array as an input anD storing a index value in a variable which is updating its values. But i am getting an error of list out of range
Please help me

Your condition while b < num-1 or b>0 does not make sense. The or b>0 part makes it True for all values larger than num-1. Instead, you should use and:
while b < num-1 and b > 0:
Or shorter, using comparison chaining:
while 0 < b < num-1:
Also, note that array indices go from 0 to num-1 (num being the length), so actually the condition should probably be (not tested, though):
while 0 <= b < num:

Related

How to compare all elements of a list [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
This code in under is will let the user enter the number of elements of list and the user need enter the elements ( User will need type number_of_elements_of_list times) and then count how many positive number, negative number, zero number. The code is like this:
the_number_of_elements_of_the_list = int(input())
number_list = []
for i in range(1, the_number_of_elements_of_the_list + 1):
number_list.append(int(input()))
positive_number_count = 0
negative_number_count = 0
zero_number_count = 0
if number_list == 0:
zero_number_count += 1
elif number_list == 1 or number_list > 1:
positive_number_count += 1
elif number_list < 0:
negative_number_count += 1
print(positive_number_count, negative_number_count, zero_number_count)
The code have a problem: The list can not compare like that. It will be error but i don't know how to compare the elements of list. Can you help me solve this problem?
Firstly, as teambob pointed out, add indent to the for-loop block.
Secondly, as DarkKnight pointed out, put the count variables outside the for-loop.
Thirdly, for each iteration, in order to use that value alone, use number_list[i-1] instead of number_list. (The index is i-1 rather than i because the range in your code starts from 1 rather than 0)
The final code would look like:
the_number_of_elements_of_the_list = int(input())
positive_number_count = 0
negative_number_count = 0
zero_number_count = 0
number_list = []
for i in range(1, the_number_of_elements_of_the_list + 1):
number_list.append(int(input()))
if number_list[i-1] == 0:
zero_number_count += 1
elif number_list[i-1] == 1 or number_list[i-1] > 1:
positive_number_count += 1
elif number_list[i-1] < 0:
negative_number_count += 1
print(positive_number_count, negative_number_count, zero_number_count)

Writing a Loop to see if a number in a list is even or odd (python) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I am trying to write a loop that goes through a list of numbers and prints whether each number is even or odd. It seemed pretty simple, but it just infinitely prints odd. What is the fallacy?
samplenumber = [1,2,3,4,5,6,7,8,9,10]
o = 0
f1 = samplenumber[o]
while f1 < 11:
if f1%2 == 0:
print ("EVEN")
else:
print ("ODD")
o += 1
You have an infinite while loop because you're incrementing the wrong variable. The while loop condition has to equal false at some point, but yours will never equal false (so it keeps running over and over again). See my comment in your code for further explanation:
samplenumber = [1,2,3,4,5,6,7,8,9,10]
o = 0
f1 = samplenumber[o]
while f1 < 11: #f1 will always equal 1 (samplenumber[o] = samplenumber[0] = 1), and 1 is always <11
if f1%2 == 0:
print ("EVEN")
else:
print ("ODD")
o += 1
You should either increment f1 in your while loop or use o as your counter variable.
Have a nice day!
you need to read up on for-loops, friend!
samplenumber = [1,2,3,4,5,6,7,8,9,10]
for i in samplenumber:
if i%2 == 0:
print ("EVEN")
else:
print ("ODD")
best of luck, cheers.
ans:
def test2():
samplenumber = [1,2,3,4,5,6,7,8,9,10]
for item in samplenumber:
if item%2 == 0:
print("EVEN")
else:
print("ODD")
Output:
ODD
EVEN
ODD
EVEN
ODD
EVEN
ODD
EVEN
ODD
EVEN

Trying to code a function with one numeric input. If the number <= 0, return -1. If the number > 0, divide that integer over-and-over by 2 [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I am new to Python and coding. This is what I have so far, but can't get it to work.
def halve_to_2( num ):
while True:
num = num /2
if num < 2:
else:
if num <=0:
print ("-1")
return num
k = int(input("Enter a number"))
print(halve_to_2(k))
def halve_to_2( num ):
while True:
if num <= 0:
print("-1")
return -1
else:
num = num / 2
k = int(input("Enter a number"))
print(halve_to_2(k))
def divisibleByTwo( num) :
if num <=0 :
return - 1
else :
divisibleByTwo (num/2)
Its called recursion. The break point is the return - 1 for this recursive loop
def divisibleByTwo( num) :
print ( num)
if int(num) > 0 :
divisibleByTwo (num/2)
return num/2
This one print the values that fall within integers not scientific decimals

Indexerror:::: list index out of range [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
hi this is my code and I don't know why I received this type of error
x = int(input())
n = [int(i) for i in input().split()]
middle = n[int((x - 1) / 2)
even = 0
odd = 0
for number in n:
if number % 2 == 0:
even += number
else:
odd += number
answer = even * odd + middle ** 2
print("{} x {} + {}^2 = {}".format(even, odd, middle, answer))
It produces an error as such IndexError: list index out of range because n has the minimum number of list values entered by the user.
Since your intention was for n to have more values than the minimum number of digits entered by the user, I added .range() to the list iteration.
Here is the modified code:
x = int(input("Put in a number: "))
n = [int(i) for i in range(int(input("Put in another number: ")))]
middle = n[int((x - 1) / 2)]
even = 0
odd = 0
for number in n:
if number % 2 == 0:
even += number
else:
odd += number
answer = even * odd + middle ** 2
print("{} x {} + {}^2 = {}".format(even, odd, middle, answer))
If you have any questions or need clarification, please do not hesitate to ask.

Multiplication without using the times or division symbol in python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
else:
while int(n2) - 1 != 0:
a = int(n) + int(n)
print("" + str(a))
I need this part of code to times n by n2 without using '*' or '/' and I'm not sure how to change this so that it will work. What do I need to change/add to make this work?
Something like this:
lowest, highest = a, b
if b < a: lowest, highest = b, a
total = 0
for _ in range(lowest):
total += highest
print "a x b = %s" % total
You can use a for loop to add n to ans exactly n2 times:
n = 30
n2 = 2
ans = 0
for i in range(n2):
ans += n
print(ans)
If you need to operate on strings (as in your question), you could use the following example:
n = '30'
n2 = '2'
ans = 0
for i in range(int(n2)):
ans += int(n)
print(str(ans))

Categories