2016-07-25 13 views
-2

私はPython(そして一般的にはコーディング)に新しく、「コンピュータ科学者のように考える:Pythonで学ぶ」という読書の約1週間後に、私は古典的な「推測ゲーム"。ユーザーが推測する数を数えたり、シミュレートされた「コンピュータ」プレーヤーと対戦してプログラムを少し面白くするなど、いくつかの追加機能を追加しました。また、コンピュータがとる推測の数は、与えられた範囲(範囲nの底2の対数)で数を推測するのに必要な推測の平均数に基づいており、標準偏差に従って変化します。私のコードの構造やコンピュータの推測数を生成する方法についてのフィードバックは非常に高く評価されます!!!Pythonの推測ゲームのコードフィードバック

Anywayyysssは....ここに私のコードはget_number(level)

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

です:

  • elifは、最初の1の後に、すべてのif表現を使用することができます。これは、式が真の場合には、後の式を評価してはならないため、実行を高速化します。 (guess_number(level)およびcheck_guess(guess,number)と同じ)
  • elifelseとすることができました。
  • get_number()とは何ですか?私はnumber = get_number(level)と書いてほしいと思うか、文全体のブロックにwhileループを使うことができます。

mainloop()whileループとは何ですか? mainloop()を呼び出してコードの繰り返し実行を実装します。しかし、私はあなたの実装に対してwhileループを好むでしょう。 yesまたはnoあなたが最初の推測と後で推測を区別しないのはなぜ?彼らはで扱うことができ、入力(ループを抜け出しと意図mainloopの終わりになって?

ではありませんどちらも場合

はbeaviourです同じコード。

+0

フィードバックに感謝します!この記事を作成した後、最初の推測とその後の推測を区別することは冗長であり、コードをこれ以上変更しないように変更しました。 f whileループは、回答(番号)を変更せずに複数の推測を可能にすることです。 mainループ()自体を呼び出すと、whileループに再入力する前に番号選択プロセスに戻り、次のラウンドのゲームで新しい番号を選択することができます。私はあなたの提案を実装して、もしかするとエリフに変えてしまいました! –