+ 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)
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.
+ 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
+ 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
+ 2
word = input()
for x in range(len(word)):
print (" "*x, word[x], sep="")
+ 2
Isn't it easier to do this through the counter?
+ 1
You can do it in your own way, just add a condition when you meet the same symbol. 😎
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.
0
O problema está na repetição da variável.
Aconselho a substituir o X.
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