2017-10-16 43 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次尝试。所以我不知道该怎么做。 python仍然有点新,所以我会喜欢一些帮助

+0

那么,为什么不改变显示方式呢? '.format(试试+ 1)' –

+0

可以工作。谢谢!! –

回答

1

任何地方你有input("What is guess #{}?\n".format(tries)),使用guess = input("What is guess #{}?\n".format(tries+1))。 (在表达式中增加+1来尝试表达式,但不能改变变量本身)