1

I am coding an nth term of a mathematical sequence generator. I'm just experimenting with the first seven terms for now.

The user enters 1,2,3,4,5,6 and 7 (or any other consecutive values) into the sequence, then the program should multiply the 1st, then every other term entered by 8 (so in this case the 1st, 3rd, 5th and 7th numbers entered).

It should also multiply the 2nd then every other term entered by 2 (so in this case the 2nd, 4th and 6th numbers entered).

Afterwards, I want it to add everything together. Here is what I tried to do:

x = [0,0,0,0,0,0,0]
for n in range (0,7):  
    x[n] = int(input("Input the digits of the sequence one by one:"))
    if x[n] == x[0] or x[2] or x[4] or x[6]:
        x[n] == x[n]*8 

    elif x[n] == x[1] or x[3] or x[5]:
        x[n] == x[n]*2 

    else:
        break

finalnumber = x[0]+x[1]+x[2]+x[3]+x[4]+x[5]+x[6]
print(finalnumber)

I inputted 1,2,3,4,5,6 and 7 and calculated myself that the finalnumber should be 152, however this program for some reason printed 28.

Please tell me what I have done wrong.

I am using Python 3.6.

3 Answers 3

1

You error derives mainly from the way you put the conditions inside your loop -

if x[n] == x[0] or x[2] or x[4] or x[6]

will check if x[n] == x[0] (which might be true even if n is not 0) or if x[2] is positive (regardless of x[n]) or if x[4] is positive and so on.

  • Note that the or separates between expressions, and is not used as in english, where a is b or c or d implies a is b, or a is c, or a is d, but a is b, or c is true, or d is true.

The correct way for comparing against multiple value is if n in [0, 2, 4, 6] that checks weather n is one of the values in that list.

However, for your case, where you want to filter odd and even numbers, there is no need for hard-coding numbers; just make the parity check - if x % 2 == 0 to cover all cases.


Now, lets review your specifications again:

  • Input n numbers.
  • Multiply odd indexes by 2.
  • Multiply even indexes by 8.
  • Sum it all.

Now python style:

n = 7
my_numbers = []

# Input n numbers.
for i in range(n):
    my_numbers.append(int(input("Enter the {} term: ".format(i))))


for i in range(n):
    # Mutiply even indexes by 8.
    if i % 2 == 0:
        my_numbers[i] = my_numbers[i] * 8
    # Mutiply odd indexes by 2.
    else:
        my_numbers[i] = my_numbers[i] * 2

print(sum(my_numbers)) # Sum it all.
Sign up to request clarification or add additional context in comments.

Comments

0

Instead of x[n] == x[n]*8 You should write

x[n] = x[n]*8

Because double equalmark returns a bool, True or False, it depends on result as mentioned in comments it is used typically with if statements, single equalmark means assigning. Of course the same situation happens when you want to multiply by 2 in second statement. I meant:

x[n] = x[n]*2

5 Comments

I downvoted this answer because of this: "double equalmark means checking whether something is equal". The operator == returns a bool, True if the values are equal and False if they are not. It is typically used in if-statements and conditionals. Although your answer may help (?) the OP, I think you should rethink your explanation completely, because it is no correct, unfortunately
I will change this explanation. Thanks for opinion.
Glad you understand! I just commented on that because I wanted to avoid confusion.
Thank you, I did that, but I am now instead getting 212. When I print each element individually, I see that the 4th (x[3]) and 6th (x[5]) values being inputted are being multiplied by 8 even though I specified in the code that they should be multiplied by 2. Why is this?
Thanks for the help, everyone!
0

Although, this is not the most pythonic way of doing this but there are two major errors with your code.

  • Firstly, inside the if conditions, you need to check for each of the values. So, replace x[n] == x[0] or x[2] or x[4] or x[6]: with if (x[n] == x[0]) or (x[n] == x[2]) or (x[n] == x[4]) or (x[n] == x[6]):

  • Secondly, you need to use the assignment operator ( = ) instead of using comparison operator ( == ) inside condition blocks. So, replace x[n] == x[n]*2 with x[n] = x[n]*2

The reason you were getting 28 as the result because x[n] = int(input("Input the digits of the sequence one by one:")) will assign values to x as [1, 2, 3, 4, 5, 6, 7] and in your if-else blocks, there is no assignment of any kind. So, x[0]+x[1]+x[2]+x[3]+x[4]+x[5]+x[6] will result in 1 + 2 + 3 + 4 + 5 + 6 + 7 which is equal to 28.

You can do something like this :

x = [0,0,0,0,0,0,0]
for n in range (0,7):  
    x[n] = int(input("Input the digits of the sequence one by one:"))
    if (x[n] == x[0]) or (x[n] == x[2]) or (x[n] == x[4]) or (x[n] == x[6]):
        x[n] = x[n]*8 

    elif (x[n] == x[1]) or (x[n] == x[3]) or (x[n] == x[5]):
        x[n] = x[n]*2 

    else:
        break

finalnumber = x[0]+x[1]+x[2]+x[3]+x[4]+x[5]+x[6]
print(finalnumber)

This will result in :

152

Alternatively, if you want to do this a bit pythonic way, you can do something like this :

x = [int(input("Input the digits of the sequence one by one : ")) for _ in range(7)]
finalnumber = sum([num * 8 if i % 2 == 0 else num * 2 for i, num in enumerate(x)])
print(finalnumber)

This will also result in :

152

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.