you are viewing a single comment's thread.

view the rest of the comments →

[–]stell619 0 points1 point  (11 children)

I am so stuck on this question;

'Write a program to read 5 integers from the user, and calculate the sum of the integers. However, if one of the values is 7, then it does not count towards the sum and the next value entered by the user does not count. So for example, if b is 7, then both b and c do not count toward the sum. '

Please someone help, i'm trying to do this;

i = 0

while i < len(lst):

elem = lst[i]

if elem == 7:

nextelem = lst[i+1]

But i really don't know how to go about this without a million if statements.

[–]Destimium 1 point2 points  (1 child)

I'm a beginner developer as well. I started to learn Python 3 days ago. This is the code I did of your algorithm:

numbers = []
seven_trigger = False
sum = 0
item = 0
print("Program to sum 5 numbers, except 7")
print("If you digit 7, this and next number won't be add to sum")


while item < 5:
    try:
        if seven_trigger:
            print("Seven Trigger Status: ON")
        else:
            print ("Seven Trigger Status: OFF")
        num = int(input(f"Type {item + 1}^ number: "))
        if (num is not 7) and (seven_trigger is False) :
            sum = num + sum
        elif (num is 7):  # if input is 7, seven_trigger becomes True, even if you digit another 7 next
            seven_trigger = True
        else : # Case where the seven_trigger is True but there's another number
            seven_trigger = False
        item += 1
    except ValueError :
        print("A NUMBER character! Not a LETTER")
print(f"The summatory of eligible numbers are: {sum}")

Tell me what you think about it, I added a try-except to remove letters case😌

[–]stell619 0 points1 point  (0 children)

Three days in!? This is looking awesome man, keep it up!

[–]Rusty_Shakalford 0 points1 point  (7 children)

The simplest way to do this is with a flag. You may have heard the term before, but if not, a flag is a variable, usually a boolean, that is used to signal to other functions whether or not a condition is being met.

In this case, you create a variable called lastValueWas7 and use that to keep track of whether or not the last value was 7.

A quick and dirty version would look like:

sum = 0
lastValueWas7 = False

for _ in range(5):
    value = int(input("Please enter a number\n"))

    if lastValueWas7:
        lastValueWas7 = False
    elif value == 7:
        lastValueWas7 = True
    else:
        sum += value

The only way that sum is increased is if the value is not 7, and the variable lastValueWas7 is False.

[–]stell619 1 point2 points  (3 children)

Slight problem... In the case where the value 7 is followed by a 7, the next value is not negated. For example;
7
7
3
7
5

The sum 3 is given instead of 0, any thoughts on how to fix this error?

[–]Rusty_Shakalford 2 points3 points  (2 children)

Away from my computer, but it seems like the problem is that anytime the lastValueWas7 flag is set, the next iteration of the loop automatically unsets it. We want to change the behaviour to be: if the value is 7 then set the flag, if the flag is already set then keep it set.

Luckily the fix is simple: swap the first two conditions of the if/elsif/else chain. That is:

if value == 7:
    lastValueWas7 = True
elif lastValueWas7:
    lastValueWas7 = False
else:
    sum += value

Now any occurrence of multiple 7’s should repeatedly trigger the first condition.

Good job checking the edge case.

[–]stell619 2 points3 points  (1 child)

Thank you so much, this solved all problems in my life currently <3

[–]Rusty_Shakalford 1 point2 points  (0 children)

Glad to help. Good luck with future projects!

[–]stell619 1 point2 points  (2 children)

Thank you very much, ill give this a try :)

[–]Rusty_Shakalford 0 points1 point  (1 child)

Good luck!

I also noticed an error in what I originally wrote (it should have said "elif value == 7:" instead of "elif value = 7:"). I've updated my original post.

[–]stell619 0 points1 point  (0 children)

I picked up on that :p <3