+ 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.
6 Antworten
+ 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)
+ 4
+ 3
https://code.sololearn.com/cQLKftq15A66/?ref=app
Hope this helps!
+ 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
0
I'm having difficulty with while loop in python. The user asks questions repeatedly until the answer provided ends the loop.