Python Forum
writing 2 up to 2**7 in phyton ( while )
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
writing 2 up to 2**7 in phyton ( while )
#1
I have a question ( not homework, im basically studying to learn phyton a little before study ):
it says to write all the numbers between 2**1 to 2**7 in phyton.
In for i saw a solution, but i didnt really understand:
for num in (1, 2, 3 , 4, 5 , 6 ,7):
     print(2**num)
and that way i got in phyton 2, 4 ,8, 16 and such.. up to 128 ( can somone explain to me please this thing? never heard of a for like that, with the () and in together.

for while, I tried something but it didnt work.. ( picture below ). but basically I did this:
num = 2
while num**2 in range(1,7):
    print(2**num)
I just kept getting 4,4,4,4,4,4 every time, cant make it to 128. any explanation please?
i tried doing the bbcode thingy, but i couldnt see where i write it, the post doesnt say where to use bbcode thingy.
Yoriz write Jul-04-2021, 08:49 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button

Attached Files

Thumbnail(s)
   
Reply
#2
for num in (1, 2, 3 , 4, 5 , 6 ,7):
     print(2**num):
(1, 2, 3 , 4, 5 , 6 ,7) is a tuple that is iterable, the for loop asigns each item in the tuple to num on each iteration.

num = 2
while num**2 in range(1,7):
    print(2**num)
in does not do the same thing when used with a while loop as it does with a for loop.
When used with while it is a conditional check that what is on the left of in is in what is on the right side of in.
num is never altered so num**2 (4) is always in the range of numbers 1 to 6
Reply
#3
(Jul-04-2021, 05:12 PM)ben1122 Wrote: it says to write all the numbers between 2**1 to 2**7 in phyton.
In for i saw a solution, but i didnt really understand:

You probably described problem incorrectly as code provided is not delivering solution. All the numbers between 2**1 and 2**7 mean 2, 3, 4,...127, 128. This code doesn't deliver that. 'between' is also ambiguous, does it mean inclusive or exclusive or like in Python (last exclusive).
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
First off, it's PYTHON, as in Monty Python. The creator of python, Guido Van Rossum, was a big fan.

Secondly, all your questions will be answered if you just read the short tutorial that Guido wrote for you. It is installed with python but you can also find it here: https://docs.python.org/3/tutorial/index.html

It's a great way to get started and will enable you to tackle simple programs and get ready for more advanced study. Check it out. Big Grin
"So, brave knights, if you do doubt your courage or your strength, come no further, for death awaits you all with nasty, big, pointy teeth!" - Tim the Enchanter
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  EYE LID RIG / PHYTON ERROR everend 0 2,293 Jun-18-2018, 10:02 AM
Last Post: everend
  Need help with phyton script Achitian 4 3,707 Dec-21-2017, 01:16 PM
Last Post: Larz60+
  Need help with my home task Phyton 3 Bazzket 1 2,649 Nov-26-2017, 05:57 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020