2016-08-23 14 views
-1

我有一個數字猜測遊戲工作,但我希望它沒有選擇數字沒有他們有一個單一的增量任何計數。我想說的是2 4 17 27不是2 3 4 17 27我希望它能像從前一樣經歷從低到高的所有可能性,而不是以任何單一增量進行IE 1 2 3或5 6編輯8月24日下午9點 - 數字遊戲不工作,因爲我想

我還希望它能夠從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()功能做了你在短短的一行,並沒有洗牌的完整列表,想要什麼:

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

相關問題