4.7 6 powers of two python

CodeHS is a comprehensive teaching platform for helping schools teach computer science. We provide web-based curriculum, teacher tools and resources, and professional development. All questions or comments related to CodeHS can go here!

0 points

about 7 years

my_list = [0,1,2,3,6,5,7]

for i in range(0,len(my_list),2**i): print my_list[i]

this code gives an error

Answer 55e1c6b5d3292fba3e0000c2

The code is fine apart from the for loop line. Read that line again. for i in range(0, len(my_list), 2**i) means to take the next value from the range and then assign it to i. So first, Python has to generate the range. This means that i can only be assigned a value when the range has been generated. This is how things will go in your case:

  1. Python sees the for loop
  2. Python tries to generate a range (it doesn’t know what i is yet)
  3. It notices that you have passed i to the range() method, it doesn’t know what i is so it generates a NameError (which basically means that it doesn’t know what i is a name of)

This is how they should have gone:

  1. Python sees the for loop
  2. It tries to generate a range. Generation is successful.
  3. It assigns the first value of the range to i

As you can see, in your case, i has not been created yet and you are already trying to pass it in as an argument to the range() function. If you tell me what you are trying to do then I could fix your code.

points

4.7 6 powers of two python

about 7 years

Answer 55e28f289113cb1cd400054d

points

4.7 6 powers of two python

about 7 years

Answer 55e3b13b937676a569000576

Try the following:

import math l = list(range(0,100)) for i in range(0, int(math.log(len(l),2))+1): print l[2**i - 1]

Note that the last line is indented but does not show in the preview.

points

about 7 years

best way with powers of 2 is using bit-shifting on 1, which is way faster than exponentiation in that case.

That said, I wouldn't recommend a while loop but rather a for loop, or even better: generate your list of values using a list comprehension (which avoids all the variables and undesired side effects, infinite loops because of while, etc...) and one-liner:

print([1<

result:

[1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536]

  • Products
  • Products
    • Coding LMS

      Manage your classroom all in one spot

    • Online IDE

      Write, run & debug code in a web-based IDE

    • CodeHS Pro

      Access a suite of teacher tools & resources

    • Computer Science Curriculum

      6-12th grade courses from intro to AP programming

    • Certifications

      Industry-relevant certifications for students

    • Professional Development

      Online & in-person training for teachers

  • Platform
    • Assignments

      Create & configure your course assignments

    • Classroom

      Manage & organize your class with customizable settings

    • Grading

      Streamline your grading workflow.

    • Data

      Track & analyze student assessments & progress data

    • Write Code

      Write, run, & debug code all in a web-based IDE

    • Integrations

      Connect CodeHS to your district’s educational platform

  • Platform
    • Assignments

      Create & configure your course assignments

    • Classroom

      Manage & organize your class with customizable settings

    • Grading

      Streamline your grading workflow.

    • Data

      Track & analyze student assessments & progress data

    • Write Code

      Write, run, & debug code all in a web-based IDE

    • Integrations

      Connect CodeHS to your district’s educational platform

  • Curriculum
  • Curriculum
    • Course Catalog
    • Pathways
    • AP Courses
    • Elementary
    • States
    • Standards
    • Practice
    • Tutorials
    • Digital Textbooks
  • Professional DevelopmentPD
  • Professional DevelopmentPD
    • Professional Development
    • Online PD Courses
    • In-Person & Virtual Workshops
    • Free PD Events
    • Teacher Certification Prep
    • Microcredentials
  • Online IDE
  • Online IDE
    • Online IDE
    • JavaScript
    • Python
    • Java
    • HTML
    • C++
    • SQL
    • Karel
  • Resources
    • About
    • Privacy Center
    • Case Studies
    • States
    • Testimonials
    • Tweets
    • Read Write Code Blog
    • Coding in the Wild
    • Knowledge Base
    • Student Projects
  • Resources
    • About
    • Privacy Center
    • Case Studies
    • States
    • Testimonials
    • Tweets
    • Read Write Code Blog
    • Coding in the Wild
    • Knowledge Base
    • Student Projects