Behaviour for Python-For loop with conditional increment (gives wrong output)

python for loop increment
while loop python
python for loop index
conditional loop in python
python for loop range
for loop python 3
for loop true, false python
for loop increment operator in python

I want a for loop that increments based on certain conditions. Here are the conditions:

The first input array contains the correct answers to an exam, like ["a", "a", "b", "d"]. The second one is "answers" array and contains student's answers.

The two arrays are not empty and are the same length. Return the score for this array of answers, giving +4 for each correct answer, -1 for each incorrect answer, and +0 for each blank answer(empty string).

def check_exam(arr1,arr2):
    for i in arr1:
        for j in arr2:
            if i==j:
            elif i!=j:
            elif i== " ":
    return counter

If every answer is correct like:

print(check_exam(["a", "a", "a", "a"], ["a", "a", "a", "a"])) it returns 64.

If two answers are wrong like:

print(check_exam(["a", "a", "a", "a"], ["b", "b", "a", "a"]))it returns 24.

Hej Jenny Two things One try and specify the problem a bit better, what I found was that your total marks where too high as it was counting in both loops. Next time try and give the expected marks as well. To solve it I made the loops loop through a range and only compare the answers of each index with each other. For example, now answer 1 in correct answers is only compared to answer 1 in student answers.

Hope this helps.

def check_exam(correct_answers, student_answers):
    counter = 0
    for i in range(0, len(correct_answers)):
        for j in range(0, len(student_answers)):
            if i == j:
                if correct_answers[i] == student_answers[j]:
                    counter += 4
                elif correct_answers[i] != student_answers[j]:
                    counter -= 1
            elif i == " ":
    return counter

print(check_exam(["a", "a", "a", "a"], ["a", "a", "a", "a"]))

print(check_exam(["a", "a", "a", "a"], ["b", "a", "a", "a"]))

4. Conditionals and loops — Beginning Python Programming for , ability to check conditions and change the behavior of the program accordingly . What happens if the condition is false, and food is not equal to 'spam' ? other than 'spam' and run it again, confirming that you don't get any output. Incrementing a variable is so common that Python provides an abbreviated syntax for it:. You can use “continue” statement inside python for loop. When a for loop encounters “continue”, it will not execute the rest of the statements in that particular for-loop-block, instead it will start the for-loop again for the next element in the list.

You are not iterating both lists at the same time - you can do this most easily like this

for solution, answer in zip(arr1, arr2):

You are not counting correctly, every time you have an empty string, you return -1 instead of 0. You need to check for the empty string first - in python thats most easily done with

if not j: // i think you need to check for j here, based on your description.
  counter += 0
  continue // forces the next loop

and then your if /else construct,.

Depending on your class, I would probably skip the loop alotger and use map and sum here.

7. Iteration — How to Think Like a Computer Scientist: Learning with , Because iteration is so common, Python provides several language features to make it easier. It is called an increment of the variable; subtracting 1 is called a decrement. Notice that if the loop condition is False the first time we get loop, the Each time through the loop, the program outputs the value of n and then� Output : 1. Such type of else is useful only if there is an if condition present inside the loop which somehow depends on the loop variable. In the following example, the else statement will only be executed if no element of the array is even, i.e. if statement has not been executed for any iteration.

Try this, use zip to iterate over two arrays at the same time.

Add 4 to the score if matches else if student answer is blank, don't do anything in that iteration else -1 from the score.

# assuming arr1 as correct answer and arr2 as student answer
>>> def check_exam(arr1,arr2):
    score = 0
    for ca, sa in zip(arr1, arr2): #ca = correct answer, sa = student answer
        if ca == sa:
        elif sa == '': # or elif not sa:
    return score

>>> print(check_exam(["a", "a", "a", "a"], ["a", "a", "a", "a"]))
>>> print(check_exam(["a", "a", "a", "a"], ["b", "b", "a", "a"]))

Python "for" Loops (Definite Iteration) – Real Python, The Guts of the Python for Loop; Iterating Through a Dictionary; The range() Indefinite iteration, in which the code block executes until some condition is met to get access to a chapter from Python Tricks: The Book that shows you Python's Technical Note: In the C programming language, i++ increments the variable i . While loops are executed based on whether the conditional statement is true or false. For loops are called iterators, it iterates the element based on the condition set; Python For loops can also be used for a set of various other things (specifying the collection of elements we want to loop over)

You don't need to implement nested loop this case so you can try this:

def check_exam(correct_answers, student_answers):
    return sum(list(map(lambda x, y: 4 if x==y else -1 if x!=y else 0 , correct_answers, student_answers)))

ProjPython – Loops and conditions, An expression that computes a True or False value is called a boolean You can also store the results of a conditional operator in a variable: You cannot put it inside a print statement, and so Python gives you an “invalid Objective: Use multiple variables in a while loop to change loop body behavior on each iteration. Python conditional statements and loops [44 exercises with solution] [An editor is available at the bottom of the page to write and execute the scripts.1. Write a Python program to find those numbers which are divisible by 7 and multiple of 5, between 1500 and 2700 (both included).

Ways to increment Iterator from inside the For loop in Python , Ways to increment Iterator from inside the For loop in Python The above example shows this odd behavior of the for loop because the time of checking condition Output: 1 3 5; Using another variable: We can use another variable for Please Improve this article if you find anything incorrect by clicking� This loop is interpreted as follows: Initialize i to 1.; Continue looping as long as i <= 10.; Increment i by 1 after each loop iteration.; Three-expression for loops are popular because the expressions specified for the three parts can be nearly anything, so this has quite a bit more flexibility than the simpler numeric range form shown above.

Loop control statements — Object-Oriented Programming in Python , If we were to write a computer program to simulate this behaviour, it would not be first time – if the condition is false at the start, the loop body will never be executed at all. The variable used in the loop condition is the number i , which you use to Then, at the end of the loop body, you update i by incrementing it by 1 . Lets take few examples of for loop to understand the usage. Python – For loop example. The following example shows the use of for loop to iterate over a list of numbers. In the body of for loop we are calculating the square of each number present in list and displaying the same.

3.3. While Statements — Hands-on Python Tutorial for Python 3, A Python while loop behaves quite similarly to common English usage. to the variables, inside the loop, that will eventually make the loop condition False ? ready for the next loop just incremented a numerical variable by a fixed amount. start, including the highest, print a sentence giving the starting value of n and the� In this tutorial, you'll learn about indefinite iteration using the Python while loop. You’ll be able to construct basic and complex while loops, interrupt loop execution with break and continue, use the else clause with a while loop, and deal with infinite loops.

  • What you want to do is to loop over arr1 and arr2 at the same time, but what you have implemented is looping over arr2 for each item of arr1.
  • Imo you don't want a nested loop, but to compare the first element of each array with each other, then the two elements with index 1, then the two with index 2..... Have a look at python's zip.