Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

There are a few problems with this code.

  • In disp_intro() you're using global HIGHSCORE, but it should actually be global HIGHSCORE_DATA
  • In case highscore.txt doesn't exist, the whole code will fail when trying to access HIGHSCORE_DATA[0] and HIGHSCORE_DATA[1], so you should have some defaults, like HIGHSCORE_DATA = ['', 0]
  • You're not closing the highscore.txt file when reading (use with) and f is not a nice name (the same goes when writing it).
  • You don't need another variable for the high score, it's already what you need
  • When reading the highscore file you probably want rstrip() to remove the extra newline you may (or may not) have

I think it's better:

if os.path.isfile('./highscore.txt'):
 with open("highscore.txt", "r") as highscore_file:
 HIGHSCORE_DATA = highscore_file.readline().rstrip().split(',')
  • In disp_intro() you also need global NUM_DIGITS
  • You don't need your own function to check if it's all digits, you have the isdigit() function
  • Put the main loop in a main() function.

Call it with:

if __name__ == '__main__':
 main()

See here here

I think the get_clues() function is a bit chaotic. It's not consistent because you're returning either a code (ERROR_LENGTH_TOO_LONG) or a description (You got it!). You're also using the same function to determine if you need to increment the difficulty or not. Also, the caller uses the returned description to check if the player has guessed correctly, which is actually the same check you're doing inside of the function.

You're probably better off having a function called compare_guess() which only returns code and the print the description of those codes using a dictionary.

If this is a school exercise and you still haven't learnt how to use dictionaries, you can still do it with two arrays, one for the codes and the other for the descriptions. I think that would still be better than mixing the two things.

There are also quite a few pep8 things to fix, but that's a minor concern right now, you should first fix the other issues.

There are a few problems with this code.

  • In disp_intro() you're using global HIGHSCORE, but it should actually be global HIGHSCORE_DATA
  • In case highscore.txt doesn't exist, the whole code will fail when trying to access HIGHSCORE_DATA[0] and HIGHSCORE_DATA[1], so you should have some defaults, like HIGHSCORE_DATA = ['', 0]
  • You're not closing the highscore.txt file when reading (use with) and f is not a nice name (the same goes when writing it).
  • You don't need another variable for the high score, it's already what you need
  • When reading the highscore file you probably want rstrip() to remove the extra newline you may (or may not) have

I think it's better:

if os.path.isfile('./highscore.txt'):
 with open("highscore.txt", "r") as highscore_file:
 HIGHSCORE_DATA = highscore_file.readline().rstrip().split(',')
  • In disp_intro() you also need global NUM_DIGITS
  • You don't need your own function to check if it's all digits, you have the isdigit() function
  • Put the main loop in a main() function.

Call it with:

if __name__ == '__main__':
 main()

See here

I think the get_clues() function is a bit chaotic. It's not consistent because you're returning either a code (ERROR_LENGTH_TOO_LONG) or a description (You got it!). You're also using the same function to determine if you need to increment the difficulty or not. Also, the caller uses the returned description to check if the player has guessed correctly, which is actually the same check you're doing inside of the function.

You're probably better off having a function called compare_guess() which only returns code and the print the description of those codes using a dictionary.

If this is a school exercise and you still haven't learnt how to use dictionaries, you can still do it with two arrays, one for the codes and the other for the descriptions. I think that would still be better than mixing the two things.

There are also quite a few pep8 things to fix, but that's a minor concern right now, you should first fix the other issues.

There are a few problems with this code.

  • In disp_intro() you're using global HIGHSCORE, but it should actually be global HIGHSCORE_DATA
  • In case highscore.txt doesn't exist, the whole code will fail when trying to access HIGHSCORE_DATA[0] and HIGHSCORE_DATA[1], so you should have some defaults, like HIGHSCORE_DATA = ['', 0]
  • You're not closing the highscore.txt file when reading (use with) and f is not a nice name (the same goes when writing it).
  • You don't need another variable for the high score, it's already what you need
  • When reading the highscore file you probably want rstrip() to remove the extra newline you may (or may not) have

I think it's better:

if os.path.isfile('./highscore.txt'):
 with open("highscore.txt", "r") as highscore_file:
 HIGHSCORE_DATA = highscore_file.readline().rstrip().split(',')
  • In disp_intro() you also need global NUM_DIGITS
  • You don't need your own function to check if it's all digits, you have the isdigit() function
  • Put the main loop in a main() function.

Call it with:

if __name__ == '__main__':
 main()

See here

I think the get_clues() function is a bit chaotic. It's not consistent because you're returning either a code (ERROR_LENGTH_TOO_LONG) or a description (You got it!). You're also using the same function to determine if you need to increment the difficulty or not. Also, the caller uses the returned description to check if the player has guessed correctly, which is actually the same check you're doing inside of the function.

You're probably better off having a function called compare_guess() which only returns code and the print the description of those codes using a dictionary.

If this is a school exercise and you still haven't learnt how to use dictionaries, you can still do it with two arrays, one for the codes and the other for the descriptions. I think that would still be better than mixing the two things.

There are also quite a few pep8 things to fix, but that's a minor concern right now, you should first fix the other issues.

Source Link
ChatterOne
  • 2.9k
  • 12
  • 18

There are a few problems with this code.

  • In disp_intro() you're using global HIGHSCORE, but it should actually be global HIGHSCORE_DATA
  • In case highscore.txt doesn't exist, the whole code will fail when trying to access HIGHSCORE_DATA[0] and HIGHSCORE_DATA[1], so you should have some defaults, like HIGHSCORE_DATA = ['', 0]
  • You're not closing the highscore.txt file when reading (use with) and f is not a nice name (the same goes when writing it).
  • You don't need another variable for the high score, it's already what you need
  • When reading the highscore file you probably want rstrip() to remove the extra newline you may (or may not) have

I think it's better:

if os.path.isfile('./highscore.txt'):
 with open("highscore.txt", "r") as highscore_file:
 HIGHSCORE_DATA = highscore_file.readline().rstrip().split(',')
  • In disp_intro() you also need global NUM_DIGITS
  • You don't need your own function to check if it's all digits, you have the isdigit() function
  • Put the main loop in a main() function.

Call it with:

if __name__ == '__main__':
 main()

See here

I think the get_clues() function is a bit chaotic. It's not consistent because you're returning either a code (ERROR_LENGTH_TOO_LONG) or a description (You got it!). You're also using the same function to determine if you need to increment the difficulty or not. Also, the caller uses the returned description to check if the player has guessed correctly, which is actually the same check you're doing inside of the function.

You're probably better off having a function called compare_guess() which only returns code and the print the description of those codes using a dictionary.

If this is a school exercise and you still haven't learnt how to use dictionaries, you can still do it with two arrays, one for the codes and the other for the descriptions. I think that would still be better than mixing the two things.

There are also quite a few pep8 things to fix, but that's a minor concern right now, you should first fix the other issues.

lang-py

AltStyle によって変換されたページ (->オリジナル) /