如何搜索和显示所选数字

时间:2016-08-29 20:21:33

标签: python numbers

好的,如果这不是最好的代码或描述,我很抱歉,我很新但很渴望学习。我有一个数字搜索游戏,我试图以几种不同的方式进行工作,但无法按照我的意愿让它工作。如果有人能指出我正确的方向,请给我一些代码,说明它正在做什么,我做错了什么,或者我可以在教程ext中找到这些信息。

感谢。

我希望它取最低数字3和最高数字111并且如果有任何连续增加[ 3,4 ,6,7]而不是[3,5,7,例如,在这种情况下,序列长度为5的数量是多少。加上用计数器生成序列的次数。

lower_limit = int(input("Please enter the lower limit: "))
upper_limit = int(input("Please enter the upper limit: "))
consecutive_length = int(input("Enter The Number Limit For Consecutive Number Increments: "))

sequence_length = int(input("Please enter the number of values in sequences: "))
count_combinations = (Count the number of times a new number combination is chosen 0)

#this is the output I would like
Please enter the lower limit: 3
Please enter the upper limit: 111
Enter The Number Limit For Consecutive Number Increments: 0
Please enter the number of values in sequences: 5

Possible sequences. Spot the missing one!

[3, 5, 7, 9, 11]
[3, 5, 7, 10, 12]
[3, 5, 7, 11, 13]
[3, 5, 7, 12, 14]ext to 111 & 3 holding the lead position
[3, 5, 8, 10, 12]
[3, 5, 8, 11, 13]
[3, 5, 8, 12, 14]ext to 111 & 3 holding the lead position
[3, 5, 9, 11, 13]
[3, 5, 9, 12, 14]
[3, 5, 10, 12, 14]
[3, 5, 10, 13, 15]ext to 111 & 3 holding the lead position

这是我的一个版本,但你可以看到输出不正确

导入itertools 随机导入

DEBUG = False
ALLOWED_GUESSES = 3


def get_game_parameters():
    while True:
        try:
            lower_limit = int(input("Please enter the lower limit: "))
            upper_limit = int(input("Please enter the upper limit: "))
            consecutive_length = int(input("Enter The Number Limit For Consecutive Increments: "))
            if lower_limit > upper_limit:
                raise ValueError("Lower limit must not exceed upper limit")

            sequence_length = int(input("Please enter the number of values in sequences: "))

            if upper_limit - lower_limit < sequence_length:
                raise ValueError("Difference in limits must be greater than sequence length")

        except ValueError as  e:
            print("Invalid value entered:", e)
            continue
        else:
            sequence_range = range(lower_limit, upper_limit + 1)
            return sequence_range, sequence_length

###input ("Enter The Number Limit For Consecutive Increments: ")

def prompt_user_to_guess(chosen_sequence, required_sequence_length):
    guesses_made = 0
    while True:
        try:
            user_input = input(
                "Please enter your guess for the hidden sequence, " +\
                "separating terms by commas (e.g. '1, 2, 3, 4'): ")

            guessed_sequence = [int(x) for x in user_input.split(",")]
            if len(guessed_sequence) != required_sequence_length:
                raise ValueError("Incorrect number of arguments")
        except ValueError as e:
            print("Invalid guess:", e)
            continue
        else:
            guesses_made += 1

            if guessed_sequence == chosen_sequence:
                print("You guessed the correct sequence. Well done!")
                return
            elif guesses_made < ALLOWED_GUESSES:
                remaining_guesses = ALLOWED_GUESSES - guesses_made
                print("Incorrect guess! {} attempt(s) remaining.".format(remaining_guesses))
                continue
            else:
                print("I'm sorry, you're out of guesses. The correct sequence was {}".format(chosen_sequence))
                return


def generate_possible_sequences(sequence_range, sequence_length):
    def is_monotonic_increasing(l):
        return all(x < y for x, y in zip(l, l[1:]))

    for permutation in itertools.permutations(sequence_range, sequence_length):
        if is_monotonic_increasing(permutation):
            yield list(permutation)


def main():
    sequence_range, sequence_length = get_game_parameters()

    # Let's hope the user doesn't select crazy ranges.
    possible_sequences = list(generate_possible_sequences(sequence_range, sequence_length))

    chosen_sequence = possible_sequences.pop(random.randrange(len(possible_sequences)))
    assert chosen_sequence not in possible_sequences

    if DEBUG:
        print("\nChosen sequence: {}".format(chosen_sequence))

    print("\nPossible sequences. Spot the missing one!\n")
    for sequence in possible_sequences:
        print(sequence)

    prompt_user_to_guess(chosen_sequence, sequence_length)


if __name__ == '__main__':
    main()

再次感谢。

0 个答案:

没有答案