+ 2

Can someone help me pls

I want to draw pattern: example for n=5 10000 01000 00100 00010 00001 Only using input, print and loops. I have no idea how to move this 1.

12th Sep 2022, 3:12 PM
GigaChad
GigaChad - avatar
6 Answers
+ 8
"""Why there are four rows when <n> was 5? (Description edited) Hint: 1. Begin with 10 raised to the power of <n> - 1 2. Divide by 10 in each loop iteration 3. Convert the value to string, then use str::zfill() to prefix the output with zeroes.""" n = int( input() or 5 ) # decimal print( "* DECIMAL:" ) val = 10 ** ( n - 1 ) while val: print( str( val ).zfill( n ) ) val //= 10 #binary print ( "\n* BINARY:" ) val = 2 << ( n - 2 ) while val: print( bin( val )[ 2 : ].zfill( n ) ) val >>= 1 (Edited)
12th Sep 2022, 3:19 PM
Ipang
13th Sep 2022, 1:14 PM
Vitaly Sokol
Vitaly Sokol - avatar
+ 3
https://code.sololearn.com/cQLKftq15A66/?ref=app Hope this helps!
13th Sep 2022, 11:41 AM
Random
Random - avatar
+ 2
# Or just rotate s = '10000' trough: s = s[-1] + s[:-1] # with a loop (and of course start to print out s in every loop). https://code.sololearn.com/cjd5w0FqEf5A/?ref=app
12th Sep 2022, 4:48 PM
Per Bratthammar
Per Bratthammar - avatar
0
I'm having difficulty with while loop in python. The user asks questions repeatedly until the answer provided ends the loop.
14th Sep 2022, 2:21 AM
Moses