import random, os HIGHSCORE_DATA = [] NUM_DIGITS = 1 MAX_GUESS = 10 if os.path.isfile('./highscore.txt'): f = open("highscore.txt", "r") highscore_data = f.readline().split(',') HIGHSCORE_DATA = highscore_data[0], highscore_data[1] def increment_difficulty(difficulty = 1): global NUM_DIGITS NUM_DIGITS += difficulty def get_secret_num(): numbers = list(range(10)) random.shuffle(numbers) secretNum = '' for i in range(NUM_DIGITS): secretNum += str(numbers[i]) return secretNum def get_clues(guess, secretNum): global NUM_DIGITS if len(guess) > NUM_DIGITS: return 'ERROR_LENGTH_TOO_LONG' if guess == secretNum: increment_difficulty() return 'You got it!' clues = [] for i in range(len(guess)): if guess[i] == secretNum[i]: clues.append('Fermi') elif guess[i] in secretNum: clues.append('pico') if len(clues) == 0: return 'Bagels' clues.sort() return ' ' . join(clues) def is_only_digits(num): # Returns True if num is a string of only digits. Otherwise, returns False if num == '': return False for i in num: if i not in '0 1 2 3 4 5 6 7 8 9'.split(): return False return True def disp_intro(): global HIGHSCORE print('[Highscore] Name: {name} | Level: {level}\n'.format(name=HIGHSCORE_DATA[0], level=HIGHSCORE_DATA[1])); print('I am thinking of a of a %s-digit number. Try to guess what ' % (NUM_DIGITS)) print('The clues I give are . . .') print('When I say: That means: ') print('Bagels None of the digits is correct.') print('Pico One digit is correct but in the wrong position.') print('Fermi One digit is correct and in the right position.') while True: NUM_DIGITS = 1 num_guesses = 1 while num_guesses <= MAX_GUESS: secret_num = get_secret_num() # Debugging Purposes - print(secret_num) print('I have thought up a number. You have %s guesses to get it.' % (MAX_GUESS)) guess = '' disp_intro() while len(guess) != NUM_DIGITS or not is_only_digits(guess): print("Guess #%s: " % (num_guesses)) guess = input() clue = get_clues(guess, secret_num) print(clue) if clue == 'You got it!': num_guesses = 1; print("============================\n") break; elif clue == 'ERROR_LENGTH_TOO_LONG': num_guesses -= 1 num_guesses += 1 #if guess == secret_num: # break; if num_guesses > MAX_GUESS: print('You ran out of guesses. The answer was %s.' % (secret_num)) print("Please enter your name (FOR HIGHSCORE): ") highscore_name = input() f = open('./highscore.txt', 'w') f.write(highscore_name + "," + str(NUM_DIGITS)) f.close() break; print('Do you want to play again? (yes or no)') if not input().lower().startswith('y'): break ---------- As the disp_intro says. It will give clues depending on the input given. > Bagels - None of the digits is correct.<br> > Pico - One digit is correct but in the wrong position.<br> > Fermi - One digit is correct and in the right position. So, basically I just want to see the most concise way this can be written as I'm pretty sure most of the code I wrote is redundant and can be improved by a lot. ---------- ## Features ## - [X] Generates random SECRET_NUM to guess. - [X] Shows current High Score. - [X] Read/Writes the High Score file. - [X] If length is too long: Do not count tries, and ask again. - [X] If it's not a number: Do not count tries, and ask again. - [X] If guess >= MAX_GUESS: Ask for player's name, and then ask to terminate. There might be more, but I think those are the most important ones.