2016-07-25 121 views
-2

我是Python新手(一般編碼),在閱讀「像計算機科學家一樣思考:用Python學習」約一週後,我決定嘗試構建一個經典的「猜測遊戲」。我添加了一些額外的功能,例如計算用戶所進行的猜測次數,並與模擬「電腦」播放器進行比賽以使該程序稍微有趣。此外,計算機猜測的次數取決於在給定範圍內猜測數字所需的平均猜測次數(以範圍n爲底數2的對數)並根據標準偏差而變化。任何關於我的代碼結構或我產生計算機猜測數量的方式的反饋都會非常感謝!python猜謎遊戲的代碼反饋

Anywayyysss ....這裏是我的代碼

import random 


def get_number(level):     #selects a random number in range depending on difficulty selected 
    if level == "e": 
     number = random.randint(1,20) 
    if level == "m": 
     number = random.randint(1,100) 
    if level == "h": 
     number = random.randint(1,1000) 
    elif level != "e" and level != "m" and level != "h": 
     print ("Invalid input!") 
     get_number() 
    return number 


def select_level():     #prompts the user to select a difficulty to play on 
    level = str(input("Would you like to play on easy, medium, or hard? \n" 
         "Type 'e' for easy, 'm' for medium, or 'h' for hard!\n")) 
    return level 


def guess_number(level):  #function that prompts the user to guess within range depending on chosen difficulty 
    if level == "e": 
     guess = int(input("Guess a number between 1 and 20:\n")) 
    if level == "m": 
     guess = int(input("Guess a number between 1 and 100:\n")) 
    if level == "h": 
     guess = int(input("Guess a number between 1 and 1000:\n")) 
    return guess 


def check_guess(guess,number):   #processes the users guess and evaluates if it is too high, too low, or bang on 
    if guess > number: 
     print ("your guess is too high! Try again! \n") 
    if guess < number: 
     print ("your guess is too low! Try again! \n") 
    if guess == number: 
     print("\n{0} was the number!".format(number)) 


def com_num_guesses(level):   #function to get the number of guesses taken by the computer 
    if level == "e": 
     com_guesses = round(random.normalvariate(3.7,1.1)) 
    if level == "m": 
     com_guesses = round(random.normalvariate(5.8,1.319)) 
    if level == "h": 
     com_guesses = round(random.normalvariate(8.99,1.37474)) 
    print("The computer guessed the number in {0} guesses! Can you beat that?".format(com_guesses)) 
    return com_guesses 


def mainloop(): 
    level = select_level() 
    number = get_number(level) 
    com_guesses = com_num_guesses(level) 
    guess = guess_number(level) 
    check_guess(guess,number) 
    num_guesses = 1 
    if guess == number:   #tells program what to do if first guess is correct 
     print("You got it in {0} guesses.".format(num_guesses)) 
     if num_guesses == com_guesses: 
      print("It took the computer {0} guesses too!\nIt's a tie!\n".format(com_guesses)) 
     if num_guesses > com_guesses: 
      print("It took the computer {0} guesses.\nThe computer wins!\n".format((com_guesses))) 
     if num_guesses < com_guesses: 
      print("It took the computer {0} guesses.\nYou win!\n".format(com_guesses)) 
     play_again = str(input("To play again type 'yes'. To exit type 'no'. \n")) 
     if play_again == "yes": 
      mainloop() 
     if play_again == "no": 
      raise SystemExit(0) 
    while True:     #tells program how to handle guesses after the first guess 
     guess2 = guess_number(level) 
     check_guess(guess2,number) 
     num_guesses += 1 
     if guess2== number: 
      print("You got it in {0} guesses.".format(num_guesses)) 
      if num_guesses == com_guesses: 
       print("It took the computer {0} guesses too!\nIt's a tie!\n".format(com_guesses)) 
      if num_guesses > com_guesses: 
       print("It took the computer {0} guesses.\nThe computer wins!\n".format((com_guesses))) 
      if num_guesses < com_guesses: 
       print("It took the computer {0} guesses.\nYou win!\n".format(com_guesses)) 
      play_again = str(input("To play again type 'yes'. To exit type 'no'. \n")) 
      if play_again == "yes": 
       mainloop() 
      if play_again == "no": 
       raise SystemExit(0) 
      break 





mainloop() 
+8

更適合代碼審查 - http://codereview.stackexchange.com/ –

+1

我投票結束這個問題作爲題外話,因爲它屬於[codereview.se] – Sayse

回答

0

get_number(level)

  • elif可以用於第一個畢竟if表達式。這會使執行速度更快,因爲在表達式爲真的情況下,不能評估後面的表達式。 (在guess_number(level)check_guess(guess,number)中相同。)
  • elif可以製成else
  • 什麼是get_number()應該做的?我想你想寫number = get_number(level)或者你可以使用while循環來完成整個語句塊。

什麼是while循環在mainloop()應該這樣做?通過在其內部調用mainloop()來實現代碼的重複執行。不過,我寧願while循環針對您的實施。

是在beaviour如果既不是或否輸入(終止循環的和獲得的mainloop打算結束?

爲什麼你的第一個猜測區分,後來猜測嗎?他們可以與處理相同的代碼

+0

感謝您的反饋意見!在做了這篇文章之後,我意識到區分第一次猜測和後續猜測並將代碼改爲不再執行此操作是多餘的。 f while循環允許在沒有答案(數字)改變的情況下進行多次猜測。在本身內部調用mainloop()允許通過在重新進入while循環之前返回號碼選擇過程來爲下一輪遊戲選擇新號碼。我實施了你的建議,把if改成elif's! –