patternpythonMinor
Lingo Guess Game
Viewed 0 times
lingogameguess
Problem
I'm writing a program for a game called "Lingo":
This is how it should work:
How can I improve this code?
def lingo():
guesslst = []
word = list("tiger")
while guesslst != word:
new = []
guess = raw_input("Enter word: ")
guesslst = list(guess)
for x in guesslst:
if x in word:
for y in word:
if x == y:
if word.index(y) == guesslst.index(x):
if x not in new:
new.append("[%s]" %x)
else:
if x not in new:
new.append("(%s)" %x)
else:
if x not in new:
new.append(x)
print("".join(new))
print("Congradulations you win! The word was %s" %"".join(word))
def main():
print("Loading word list from file...")
print("Welcome to the game, Lingo!")
lingo()
if __name__ == "__main__": main()This is how it should work:
Welcome to the game, Lingo!
Enter word: snake
snak(e)
Enter word: fiest
fis(t)
Enter word: times
[t][i]m[e]s
Enter word: tiger
[t][i][g][e][r]
Congradulations you win! The word was tiger
How can I improve this code?
Solution
Various details
The first things caught by my eye are : an akward indentation, a typo in "Congratulations" and the inlined call to main.
Then, in the slightly less visible details, you are missing a whitespace after the operator
Finally, in the invisible details, you have trailing whitespaces.
After getting this fixed, you have :
Subtle bug
At the moment, when the initial
A matter of preference
This is purely personal but I do not quite like having to initialise
Types
You do not need to convert your original
Code organisation
It can easily be seen than nothing will happen for a given
The first things caught by my eye are : an akward indentation, a typo in "Congratulations" and the inlined call to main.
Then, in the slightly less visible details, you are missing a whitespace after the operator
%.Finally, in the invisible details, you have trailing whitespaces.
After getting this fixed, you have :
def lingo():
guesslst = []
word = list("tiger")
while guesslst != word:
new = []
guess = raw_input("Enter word: ")
guesslst = list(guess)
for x in guesslst:
if x in word:
for y in word:
if x == y:
if word.index(y) == guesslst.index(x):
if x not in new:
new.append("[%s]" % x)
else:
if x not in new:
new.append("(%s)" % x)
else:
if x not in new:
new.append(x)
print("".join(new))
print("Congratulations you win! The word was %s" % "".join(word))
def main():
print("Loading word list from file...")
print("Welcome to the game, Lingo!")
lingo()
if __name__ == "__main__":
main()Subtle bug
At the moment, when the initial
word contains a duplicated letter ("tigert" for instance), you code does not quite work.A matter of preference
This is purely personal but I do not quite like having to initialise
guesslst to an empty list only to make the test for the first iteration go fine. I'd rather write something like :while True:
guesslst = ...
...
if guesslst == word:
breakTypes
You do not need to convert your original
word into a list : for and in will work just as well on a string. Similarly, you do not need to convert the guesses into a list. This makes a lot of code removable :def lingo():
word = 'tiger'
#while True:
# guess = raw_input("Enter word: ")
for guess in ['tige', 'monkey', 'a', 'i', 'tigre', 'tiger', 'tigert', 'foo']:
new = []
for x in guess:
if x in word:
for y in word:
if x == y:
if word.index(y) == guess.index(x):
if x not in new:
new.append("[%s]" % x)
else:
if x not in new:
new.append("(%s)" % x)
else:
if x not in new:
new.append(x)
print("".join(new))
if guess == word:
print("Congratulations you win! The word was %s" % word)
breakCode organisation
It can easily be seen than nothing will happen for a given
x if it is already in new. We might as well extract the corresponding logic in a single place :for x in guess:
if x not in new:
if x in word:
for y in word:
if x == y:
if word.index(y) == guess.index(x):
new.append("[%s]" % x)
else:
new.append("(%s)" % x)
break
else:
new.append(x)Code Snippets
def lingo():
guesslst = []
word = list("tiger")
while guesslst != word:
new = []
guess = raw_input("Enter word: ")
guesslst = list(guess)
for x in guesslst:
if x in word:
for y in word:
if x == y:
if word.index(y) == guesslst.index(x):
if x not in new:
new.append("[%s]" % x)
else:
if x not in new:
new.append("(%s)" % x)
else:
if x not in new:
new.append(x)
print("".join(new))
print("Congratulations you win! The word was %s" % "".join(word))
def main():
print("Loading word list from file...")
print("Welcome to the game, Lingo!")
lingo()
if __name__ == "__main__":
main()while True:
guesslst = ...
...
if guesslst == word:
breakdef lingo():
word = 'tiger'
#while True:
# guess = raw_input("Enter word: ")
for guess in ['tige', 'monkey', 'a', 'i', 'tigre', 'tiger', 'tigert', 'foo']:
new = []
for x in guess:
if x in word:
for y in word:
if x == y:
if word.index(y) == guess.index(x):
if x not in new:
new.append("[%s]" % x)
else:
if x not in new:
new.append("(%s)" % x)
else:
if x not in new:
new.append(x)
print("".join(new))
if guess == word:
print("Congratulations you win! The word was %s" % word)
breakfor x in guess:
if x not in new:
if x in word:
for y in word:
if x == y:
if word.index(y) == guess.index(x):
new.append("[%s]" % x)
else:
new.append("(%s)" % x)
break
else:
new.append(x)Context
StackExchange Code Review Q#64206, answer score: 2
Revisions (0)
No revisions yet.