For one and all


#1

I get an error "It looks like your loop did not print 2!"

my_list = [1,9,3,8,5,7]

for number in my_list:
    number = (2 * my_list) 
    print number

#2

for number in my_list:
    a =  2 *  number 
    print number

first we create a variable to store our new values, let it be 'a'. The for loop here stands for "for each", so we're telling the computer for each number in my list, give this variable the value of double the number, then print it. we dont need a variable to store it we could just do

print 2 * number

#3

Hi,

Thank you for your reply.
I have a question. But if we say

print number

at the end of the command doesn't it print just the numbers that were stored in my_list but not the result of 2*number which is stored in a?


#4

Sorry, but it does only print the numbers in my_list. It should be

print a


#5

or you don't store the result in a variable but print it directly like @netsolver29560 suggested? But yes, if you want to print the result of the multiplication, you need to print a

The problem with your code was that you used the same name for your iterator and the variable you stored the result of the multiplication. This doesn't work.


#6

thank you. will keep that in mind.


#7

i forgot to edit his part of print number sorry I goofed up :confused:


#8

no problem, that can happen :wink:


#9

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.