Hi everyone,
Newbi here. I'm writting this function, which is technically working, but still I want "i" to increase by 1 on each loop, but it won't let it. It only takes one parameter per loop. And if I change the variable name "i" -> "it", the while sentence won't even start.
** Thanks in advance for being cool about it!**
`
def solution(s):
i = 0
m = ""
braille = {
" " : "000000",
"^" : "000001",
"a" : "100000",
"b" : "110000",
"c" : "100100",
"d" : "100110",
"e" : "100010",
"f" : "110100",
"g" : "110110",
"h" : "110010",
"i" : "010100",
"j" : "010110",
"k" : "101000",
"l" : "111000",
"m" : "101100",
"n" : "101110",
"o" : "101010",
"p" : "111100",
"q" : "111110",
"r" : "111010",
"s" : "011100",
"t" : "011110",
"u" : "101001",
"v" : "111001",
"w" : "010111",
"x" : "101101",
"y" : "101111",
"z" : "101011"
}
while i < len(s):
for i in s:
if i.isupper() == True:
m = braille["^"] braille[i.lower()]
else:
m = braille[i]
print(m)
solution("hello")
`
It works, but the iterator variable won't increase on each loop
CodePudding user response:
I am not sure if this is what you want to print but let me try it out: (I assume you want to print braille pattern)
first you are making mistake of using i in the while loop as index but uses also i for index in for loop in s.
Then you also failed to increment the i in the while loop
also put newline for each new while loop:
here is my code (please check):
def solution(s):
i = 0
m = ""
braille = {
" " : "000000",
"^" : "000001",
"a" : "100000",
"b" : "110000",
"c" : "100100",
"d" : "100110",
"e" : "100010",
"f" : "110100",
"g" : "110110",
"h" : "110010",
"i" : "010100",
"j" : "010110",
"k" : "101000",
"l" : "111000",
"m" : "101100",
"n" : "101110",
"o" : "101010",
"p" : "111100",
"q" : "111110",
"r" : "111010",
"s" : "011100",
"t" : "011110",
"u" : "101001",
"v" : "111001",
"w" : "010111",
"x" : "101101",
"y" : "101111",
"z" : "101011"
}
while i < len(s):
m = '\n' # add new line for the next part of the print
for k in s: # change i to k as index in for loop
if k.isupper() == True:
m = braille["^"] braille[k.lower()]
else:
m = braille[k]
i = 1 # increment the i for the while loop
print(m)
solution("hello")
my run result:
110010100010111000111000101010
110010100010111000111000101010
110010100010111000111000101010
110010100010111000111000101010
110010100010111000111000101010
is this pattern you wish to print?
If what you want is just to print the braille pattern once then you don't need the while loop. The for loop will be sufficient :
def solution(s):
i = 0
m = ""
braille = {
" " : "000000",
"^" : "000001",
"a" : "100000",
"b" : "110000",
"c" : "100100",
"d" : "100110",
"e" : "100010",
"f" : "110100",
"g" : "110110",
"h" : "110010",
"i" : "010100",
"j" : "010110",
"k" : "101000",
"l" : "111000",
"m" : "101100",
"n" : "101110",
"o" : "101010",
"p" : "111100",
"q" : "111110",
"r" : "111010",
"s" : "011100",
"t" : "011110",
"u" : "101001",
"v" : "111001",
"w" : "010111",
"x" : "101101",
"y" : "101111",
"z" : "101011"
}
for k in s: # change i to k as index in for loop
if k.isupper() == True:
m = braille["^"] braille[k.lower()]
else:
m = braille[k]
print(m)
solution("hello")
Then the output will be:
110010100010111000111000101010