patternpythonMinor
Beginner word-guessing game
Viewed 0 times
beginnergamewordguessing
Problem
The aim is to guess a four letter string. Each letter can be tried four times. If the letter cannot be guessed, we go to the next letter until the fourth letter is reached.
I looked at my code and even though I'm satisfied because I've done everything by myself, I'm wondering how I can improve it. I'm probably relying too much on
```
# -- coding: utf-8 --
def typeYourword():
global yourword
yourword= raw_input("type your word \n")
while len(yourword)4:
yourword= raw_input("type your word again \n")
else:
print("Now your word : ",yourword," has the correct format")
return yourword
#yourword='tata'
def monMot(tonmot):
global var_glob
var_glob=list(tonmot)
return var_glob
def comptage(monmot):
global var_comptage
var_comptage = 0
for cpte in range(0,(len(monmot))):
print("this is your " + str(cpte+1) + " letter")
for count in range(1,5):
comptage_mot=cpte+1
b = raw_input("Type your letter \n")
while len(b)>1 or len(b)==0:
b=raw_input("type your letter again \n")
else:
if var_glob[cpte] == b and comptage_mot<4:
var_comptage+=1
print ("yes")
break
elif var_glob[cpte] == b and comptage_mot==4:
var_comptage+=1
print ("You won. End of game")
break
else:
if count == 4 and comptage_mot<4:
print("this was your "+str(count)+ " last try for the "+str(comptage_mot)+" letter. let's go to the next letter")
elif count == 4 and comptage_mot==4:
print("this was your last try for the word. You guessed "+str(count)+ " letters. End of game.")
else:
print("wrong letter, try again" + " ,this was your " + str(count) + " try. Now your " +str(count+1) + " try")
print(var_comptage)
typeYourword()
monMot(yourword)
comptage(va
I looked at my code and even though I'm satisfied because I've done everything by myself, I'm wondering how I can improve it. I'm probably relying too much on
if but so far. I do not have ideas on what I can do to make it leaner.```
# -- coding: utf-8 --
def typeYourword():
global yourword
yourword= raw_input("type your word \n")
while len(yourword)4:
yourword= raw_input("type your word again \n")
else:
print("Now your word : ",yourword," has the correct format")
return yourword
#yourword='tata'
def monMot(tonmot):
global var_glob
var_glob=list(tonmot)
return var_glob
def comptage(monmot):
global var_comptage
var_comptage = 0
for cpte in range(0,(len(monmot))):
print("this is your " + str(cpte+1) + " letter")
for count in range(1,5):
comptage_mot=cpte+1
b = raw_input("Type your letter \n")
while len(b)>1 or len(b)==0:
b=raw_input("type your letter again \n")
else:
if var_glob[cpte] == b and comptage_mot<4:
var_comptage+=1
print ("yes")
break
elif var_glob[cpte] == b and comptage_mot==4:
var_comptage+=1
print ("You won. End of game")
break
else:
if count == 4 and comptage_mot<4:
print("this was your "+str(count)+ " last try for the "+str(comptage_mot)+" letter. let's go to the next letter")
elif count == 4 and comptage_mot==4:
print("this was your last try for the word. You guessed "+str(count)+ " letters. End of game.")
else:
print("wrong letter, try again" + " ,this was your " + str(count) + " try. Now your " +str(count+1) + " try")
print(var_comptage)
typeYourword()
monMot(yourword)
comptage(va
Solution
Firstly, your function names do not conform to standards. For example,
You should read PEP 8, or set your GUI (If you are using one) to warn you of violations of the standard. For example, you are missing a lot of spaces around your operators. For example,
Your indent length varies between different functions making the code a bit harder to reason about.
The use of global variables is frowned upon. You are already returning the variable from
You do checks for greater than or less than when you could just be using the
Code that is run when the module is loaded directly (the last three lines) should be contained within a check for
You do not need the
Hopefully this can help you, and comment if you want anything I've said to be clarified
typeYourword should be typeYourWord, thought I'd suggest actually using something similar to inputWord, so the verb is related to what the program does rather than the user.You should read PEP 8, or set your GUI (If you are using one) to warn you of violations of the standard. For example, you are missing a lot of spaces around your operators. For example,
b=raw_input("type your letter again \n") should be b = raw_input("type your letter again \n").Your indent length varies between different functions making the code a bit harder to reason about.
The use of global variables is frowned upon. You are already returning the variable from
typeYourword, so consider storing this in a variable and passing it into the other functions. For example:word = typeYourword()
monmot(word)monmot appears to have no function, as strings can already be indexed, though perhaps I am not seeing something there.You do checks for greater than or less than when you could just be using the
!= operator to check for inequality (while len(yourword) != 4)Code that is run when the module is loaded directly (the last three lines) should be contained within a check for
__name__ == "__main__"You do not need the
else clause on the while loops, this is mainly for complicated loops involving break statements, and you can just outdent your code: while len(yourword)4:
yourword= raw_input("type your word again \n")
print("Now your word : ",yourword," has the correct format")
return yourwordHopefully this can help you, and comment if you want anything I've said to be clarified
Code Snippets
word = typeYourword()
monmot(word)while len(yourword)<4 or len(yourword)>4:
yourword= raw_input("type your word again \n")
print("Now your word : ",yourword," has the correct format")
return yourwordContext
StackExchange Code Review Q#121584, answer score: 6
Revisions (0)
No revisions yet.