Reputation: 2936
I wrote a code that is based upon some word tricks. There is a predefine word in my code. The code asks for user input. Then it compare between the predefine word and user input. If they are same it will produce an output.
My code:
letter_list=[]
words="admit"
i=0
while i< len(words):
user = input("Letter: ")
if user in words:
letter_list.append(user)
i=i+1
else:
i=i+1
if len(words)== len(letter_list):
print("Same word")
else:
print(letter_list)
Output(if they got same input similar to words):
Letter: m
Letter: i
Letter: a
Letter: t
Letter: d
Same word
Output (When given letter not similar to words )
Letter: r
Letter: t
Letter: m
Letter: a
Letter: k
['t', 'm', 'a']
Although the code gave the output same word
but it can be easily understandable that they are not. I gave the same letters as user input at the time of compilation but they are not in order. But my code says they are same. In the case of second input it only takes similar letter of words. So the number of letter in the list is 3.
I want to know that how could it takes input sequentially?
I mean when I will give m
or i
after a
it will not take m
or i
and prompt wrong letter until it will get d
.
Upvotes: 0
Views: 164
Reputation: 782105
Check if the user's letter is the current character in words
. If not, print "wrong letter" and don't increment i
, so it will keep waiting for the user to enter the correct letter.
You don't need letter_list
, since you won't get out of the loop until the user enters all the correct letters.
while i < len(words):
user = input("Letter: ")
if user == words[i]:
i=i+1
else:
print("Wrong letter")
print("Same word")
Upvotes: 1