+ 4

What is wrong with this?

It should print every Letter times its Position in the String! But it does not work always. If a letter repeats in the word then we have Problem. What should I do? word = str(input()) for x in word: m = word.index(x)+1 print (x * m)

25th Sep 2022, 10:54 PM
Aref Ahmadi
Aref Ahmadi - avatar
10 ответов
+ 11
Ism Familiya , please do not post some code that has no relation to the current question. if you have a coding related question, please create your own post.
3rd Nov 2022, 2:54 PM
Lothar
Lothar - avatar
+ 3
Aref Ahmadi , the issue with the current code is that it uses .index(). index() gets always the first occurrence of the searched character in a sequence, so duplicated characters don't get their correct index. instead of this, we can use a variable `ind` that is used as an index: word = str(input()) ind = 1 # <<< use a variable that holds an index for x in word: #m = word.index(x)+1 # not required print (x * ind) ind +=1 # <<< increment index variable
26th Sep 2022, 10:36 AM
Lothar
Lothar - avatar
+ 3
Aref Ahmadi As mentioned by Lothar , .index() always gets the first occurance of a character in a string and therefore problems arises in case of repeat chars. It is not possible to complete the task without modification
26th Sep 2022, 3:54 PM
Sandeep
Sandeep - avatar
+ 3
Lothar Sandeep Thank you guys. That is what I wanted to know. By the way I just found out that I should use Tag to Kind of answering a comment.
26th Sep 2022, 6:24 PM
Aref Ahmadi
Aref Ahmadi - avatar
+ 2
word = input() for x in range(len(word)): print (" "*x, word[x], sep="")
26th Sep 2022, 12:13 AM
SoloProg
SoloProg - avatar
+ 2
Isn't it easier to do this through the counter?
26th Sep 2022, 12:23 AM
Solo
Solo - avatar
+ 1
You can do it in your own way, just add a condition when you meet the same symbol. 😎
26th Sep 2022, 5:05 PM
Solo
Solo - avatar
0
With all respct, when I ask what is wrong with my code, I mean I want to get to the result with my own aproach. Of course you can solve this Problem in different ways but I want to know if it is possible to solve it in my way.
26th Sep 2022, 2:49 PM
Aref Ahmadi
Aref Ahmadi - avatar
0
O problema está na repetição da variável. Aconselho a substituir o X.
27th Sep 2022, 3:08 PM
Datan Aguiar
Datan Aguiar - avatar
0
<!DOCTYPE html> <html> <head> <style> .form { border: 1px solid black; width: 300px; padding: 100px; } </style> </head> <body> <form class="form"> <h1>Kirish</h1> <div class="input__group"> <input type="email" placeholder="Email"> <br> <br> <input type="password" placeholder="Parol"> <br> </div> <div class="checkbox__group"> <input type="checkbox">Shartlarni qabul qilish <br> <input type="checkbox">Eslab qolish
3rd Nov 2022, 4:42 AM
Ism Familiya
Ism Familiya - avatar