2017-10-16 9 views
0

ゲームを実行して推測を開始するたびに、推測#0とは何かが最初に尋ねられますか?私はそれに "#1の推測は何ですか?"と表示しようとしています。しかし。推測の数を推測された数と同じに保ちます(それが理にかなっている場合)。これまでのコードは、これまでのところです:試行回数を1から0以外で試してみよう

import random 

def play_game(name, lower=1, upper=10): 
    secret = random.randint(lower, upper) 
    tries = 0 

    print("-----------------------------\n" 
      "Welcome, {}!\n" 
      "I am thinking of a number\n" 
      "between {} and {}.\n" 
      "Let's see how many times it\n" 
      "will take you to guess!\n" 
      "-----------------------------".format(name, lower, upper)) 

    # Main loop 
    guessing_numbers = True 
    while guessing_numbers: 
     guess = input("What is guess #{}?\n".format(tries)) 


     while not guess.isdigit(): 
      print("[!] Sorry, that isn't a valid input.\n" 
        "[!] Please only enter numbers.\n") 
      guess = input("What is guess #{}?\n".format(tries)) 

     guess = int(guess) 
     tries += 1 

     if guess < secret: 
      print("Too low. Try again!") 
     elif guess > secret: 
      print("Too high. Try again!") 
     else: 
      guessing_numbers = False 

    if tries == 1: 
     guess_form = "guess" 
    else: 
     guess_form = "guesses" 

    print("--------------------------\n" 
      "Congratulations, {}!\n" 
      "You got it in {} {}!\n" 
      "--------------------------\n".format(name,tries,guess_form)) 

    if tries < 3: 
     # Randomly chooses from an item in the list 
     tries_3 = ["Awesome job!","Bravo!","You rock!"] 
     print (random.choice(tries_3)) 
     # --- 
    elif tries < 5: 
     tries_5 = ["Hmmmmmpff...","Better luck next time.","Ohhh c'mon!  You can do better than that."] 
     print (random.choice(tries_5)) 
    elif tries < 7: 
     tries_7 = ["You better find something else to do..","You can do better!","Maybe next time..."] 
     print (random.choice(tries_7)) 
    else: 
     tries_8 = ["You should be embarrassed!","My dog could do better. Smh...","Even I can do better.."] 
     print (random.choice(tries_8)) 

    choice = input("Would you like to play again? Y/N?\n") 
    if "y" in choice.lower(): 
     return True 
    else: 
     return False 

def main(): 
    name = input("What is your name?\n") 
    playing = True 
    while playing: 
     playing = play_game(name) 


if __name__ == "__main__": 
    main() 

私は最初に "試行回数"を0に設定しています。しかし、それを1に設定してゲームをプレイすると3回の試行しかないので、4回試してみると表示されます。だから私は何をすべきか分からない。私はまだいくつかの助けを歓迎します。

+0

それを表示する方法を変更するだけではどうですか? '.format(tries + 1)' –

+0

が動作します。ありがとう!! –

答えて

1

あなたが持っているinput("What is guess #{}?\n".format(tries))のどこでも、guess = input("What is guess #{}?\n".format(tries+1))を使用してください。 (式を試すのに+1を加えても、変数自体は変更しない)

関連する問題