对硬币翻转项目的反馈-初学者

时间:2019-12-29 21:38:49

标签: python-3.x while-loop project feedback

我想我终于完成了一个网上发现的小型硬币翻转项目。但是,我非常有信心这不是最佳解决方案,并且非常感谢您提供有关如何改善此问题的反馈。由于我是使用Head First和Code Academy自己学习的,因此反馈将非常有帮助!

挑战的目标是:

  • 作为用户,我希望能够猜测随机掷硬币(头/尾)的结果。
  • 作为用户,我想清楚地看到硬币翻转的结果。
  • 作为用户,我想清楚地知道我是否猜对了。
  • 作为用户,我想清楚地查看更新的猜测历史记录(正确的计数/总计数)。
  • 作为用户,我希望能够在每个周期后退出游戏或再次玩游戏。

这是我第一次真正使用Git Hub,但是我包含了.py文件,希望您可以在此处访问:https://github.com/adarmec/coin_flip_challenge

如果更简单,我还将代码包含在帖子中

import random


def random_flip(): #function to generate random Heads or Tails output

    random_choice = random.choice(["Heads","Tails"])
    return random_choice

def user_choice(): #function to ensure a valid input from the user, and returns that input.

    user_input = ""
    valid_guesses = ["Heads", "Tails"]

    while user_input not in valid_guesses:
        user_input = input("Heads or Tails?")

    return user_input


def coin_flip_game(): #function that plays the coin flip game.

    guess_count = 1  #guesses set to 1 so that program considers 1st guess.
    guesses = []     #list of guesses made by user
    winner = False
    first_try = True      #Variable to determine whether first try of user or not
    cancel_game = False   #Variable to determine whether game was cancelled

    print("Lets start! Begin by choosing:")

    while winner == False:
        if first_try == True:
            user_input = user_choice()

            if user_input != random_flip():
                guess_count += 1
                guesses.append(user_input)
                first_try = False
            else:
                winner = True
        else:
            decision = input("You guessed wrong, do you want to continue? Yes or No ")

            if decision == "Yes":
                user_input = user_choice()

                if user_input != random_flip():
                    guess_count += 1
                    guesses.append(user_input)
                    first_try = False
                else:
                    winner = True
            else:
                winner = True
                cancel_game = True
                break

    if cancel_game == True:
        print("Game over")
    elif guess_count == 1:
        print("Congratulations, you guessed correctly on your first try!")
    else:
        print("Congratulations! After ", guess_count, "you guessed correctly!")
        print("Your previous guesses were: ", guesses)

coin_flip_game()


0 个答案:

没有答案