2016-08-23 5 views
-1

数字のゲームが動作していますが、数字を1つ増やすことなく数字を選択することはできません。私が言おうとしているのは2 4 17 27ではない2 3 4 17 27私はそれがそうであるように低から高へのすべての可能性を通り抜けたいと思います。編集されたaug 24 9pm - 私が望むように数字のゲームが動作しません

私はまた、1から100、カウントの長さを10にすることができるようにしたいと思います。私が試してみると、6以上を入れても機能しません。

It gives me a result like this now 
Please enter the lower limit: 2 
Please enter the upper limit: 9 
Please enter the number of values in sequences: 6 

Possible sequences. Spot the missing one! 

    [2, 3, 4, 5, 6, 7] #I do not want it to have them all 1 digit up at a time 
    [2, 3, 4, 5, 6, 8] #like this 
    [2, 3, 4, 5, 6, 9] 
    [2, 3, 4, 5, 7, 8] 
    [2, 3, 4, 5, 7, 9] 
    [2, 3, 4, 5, 8, 9] 
    [2, 3, 4, 6, 7, 8] 
    [2, 3, 4, 6, 7, 9] 
    [2, 3, 4, 6, 8, 9] 
    [2, 3, 4, 7, 8, 9] 
    [2, 3, 5, 6, 7, 8] 
    [2, 3, 5, 6, 7, 9] 
    [2, 3, 5, 6, 8, 9] 
    [2, 3, 5, 7, 8, 9] 
    [2, 3, 6, 7, 8, 9] 



    Please enter the lower limit: 2 
    Please enter the upper limit: 13 
    Please enter the number of values in sequences: 6 

    [2, 4, 6, 8, 10] #I would like it more like this 
    [2, 4, 7, 9, 11] #so it go's through low to high but with out 1 digit 
    [2, 5, 7, 9, 11] #increments 
    [2, 5, 9, 11, 13] 
    [2, 6, 9, 11, 13] 
    [2, 7, 9, 11, 13,] 

ご意見がありましたら教えてください。

import itertools 
import random 

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 + 2) 
      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() 

    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() 

答えて

1

正確に回答しているかどうかは不明で、「うまくいかない」という説明が必要です。しかし、generate_possible_sequences()にはあまりにも多くのオプションを生成しているようで、おそらくメモリがいっぱいになるか、実行時間が長すぎます。

sequence_range, sequence_length = get_game_parameters() 
vals = range(1, sequence_range + 1) 
random.shuffle(vals) 
chosen_sequence = sorted(vals[:sequence_length]) 

これは良く数千人にsequence_range値について、ほぼ瞬時に動作します、そして、あなたが見て対話的にそれをステップ実行することができます:より良いアプローチは、そうのように、その後、サブシーケンスを並べ替える、ちょうど範囲で数字をシャッフルすることですどのようにコードが動作している。しかし、要素(または全体的に単に短いコード)の数百万人のために、random.sample()機能はあなただけの1行目に、完全なリストをシャッフルすることなく、欲しいものを行います。ここ

sequence_range, sequence_length = get_game_parameters() 
chosen_sequence = sorted(random.sample(xrange(1, sequence_range + 1), sequence_length)) 

xrange()は、メモリ内の実際のリストを作成する回避。このラインは、メモリ不足エラーを与える:

random.sample(range(1, 999999999), 4) 

この行は動作します:

random.sample(xrange(1, 999999999), 4) 

あなたはおそらくも欲しい:

guessed_sequence.sort() 

これは、任意の順序で入力するユーザーを可能にします。

+0

助けてくれてありがとう – Ronald

+0

私はあなたが示唆したコードに変更を加えようとしましたが、私は正しく行ったかどうかはわかりません。私はちょうどそれを実行する必要があるときに入力を尋ねることを持っているだけで、入力を再度尋ねます。 – Ronald

関連する問題