Skip to content

Latest commit

 

History

History
298 lines (205 loc) · 11.7 KB

beginner-friendly-python-projects-fun.md

File metadata and controls

298 lines (205 loc) · 11.7 KB

初学者友好的有趣 Python 项目!

原文:www.kdnuggets.com/2022/10/beginner-friendly-python-projects-fun.html

初学者友好的有趣 Python 项目!

Andrey Metelev 通过 Unsplash

我最近做了一篇关于在不到 5 分钟内构建 Python 项目的文章。所以我决定再做一篇,提供更多供初学者玩耍和测试技能的项目。


我们的前三大课程推荐

1. 谷歌网络安全证书 - 快速进入网络安全职业生涯。

2. 谷歌数据分析专业证书 - 提升你的数据分析技能

3. 谷歌 IT 支持专业证书 - 支持你组织的 IT


如果你在职业生涯中没有乐趣,你很快会失去激情并开始讨厌它。像这样的项目不仅适合初学者,还能为你的学习或职业增添一点乐趣。

现在我们开始吧。

猜数字

在这个第一个项目中,我们将生成一个特定范围内的随机数字,用户需要通过提示来猜测。

用户猜错的次数越多,给出的提示就会越多——但这会减少他们的得分。

代码:

""" Guess The Number """
import random
attempts_list = []
def show_score():
    if len(attempts_list) <= 0:
        print("There is currently no high score, it's yours for the taking!")
    else:
        print("The current high score is {} attempts".format(min(attempts_list)))
def start_game():
    random_number = int(random.randint(1, 10))
    print("Hello traveler! Welcome to the game of guesses!")
    player_name = input("What is your name? ")
    wanna_play = input("Hi, {}, would you like to play the guessing game? (Enter Yes/No) ".format(player_name))
    #Where the show_score function USED to be
    attempts = 0
    show_score()
    while wanna_play.lower() == "yes":
        try:
            guess = input("Pick a number between 1 and 10 ")
            if int(guess) < 1 or int(guess) > 10:
                raise ValueError("Please guess a number within the given range")
            if int(guess) == random_number:
                print("Nice! You got it!")
                attempts += 1
                attempts_list.append(attempts)
                print("It took you {} attempts".format(attempts))
                play_again = input("Would you like to play again? (Enter Yes/No) ")
                attempts = 0
                show_score()
                random_number = int(random.randint(1, 10))
                if play_again.lower() == "no":
                    print("That's cool, have a good one!")
                    break
            elif int(guess) > random_number:
                print("It's lower")
                attempts += 1
            elif int(guess) < random_number:
                print("It's higher")
                attempts += 1
        except ValueError as err:
            print("Oh no!, that is not a valid value. Try again...")
            print("({})".format(err))
    else:
        print("That's cool, have a good one!")
if __name__ == '__main__':
    start_game()

猜字谜

猜字谜的重点是选择一个单词,所以首先我们需要找到一个单词列表。在 StackOverflow 上,有一个包含 2400 多个单词的 JSON 文件。你可以在这里找到它:randomlist

使用这个 JSON 文件,将这些单词复制到一个 .py 文件中,并将其分配给变量 ‘words’。像这样:

words = "aback","abaft","abandoned","abashed","aberrant","abhorrent"...

代码:

创建一个第二个 .py 文件,命名为 hangman.py,它将包含这些内容:

""" Hangman """

#Imports
​​import random
from words import words
from hangman_visual import lives_visual_dict
import string

def get_valid_word(words):
    word = random.choice(words)  # randomly chooses something from the list
    while '-' in word or ' ' in word:
        word = random.choice(words)

    return word.upper()

def hangman():
    word = get_valid_word(words)
    word_letters = set(word)  # letters in the word
    alphabet = set(string.ascii_uppercase)
    used_letters = set()  # what the user has guessed

    lives = 7

    # getting user input
    while len(word_letters) > 0 and lives > 0:
        # letters used
        # ' '.join(['a', 'b', 'cd']) --> 'a b cd'
        print('You have', lives, 'lives left and you have used these letters: ', ' '.join(used_letters))

        # what current word is (ie W - R D)
        word_list = [letter if letter in used_letters else '-' for letter in word]
        print(lives_visual_dict[lives])
        print('Current word: ', ' '.join(word_list))

        user_letter = input('Guess a letter: ').upper()
        if user_letter in alphabet - used_letters:
            used_letters.add(user_letter)
            if user_letter in word_letters:
                word_letters.remove(user_letter)
                print('')

            else:
                lives = lives - 1  # takes away a life if wrong
                print('\nYour letter,', user_letter, 'is not in the word.')

        elif user_letter in used_letters:
            print('\nYou have already used that letter. Guess another letter.')

        else:
            print('\nThat is not a valid letter.')

    # gets here when len(word_letters) == 0 OR when lives == 0
    if lives == 0:
        print(lives_visual_dict[lives])
        print('You died, sorry. The word was', word)
    else:
        print('YAY! You guessed the word', word, '!!')

if __name__ == '__main__':
    hangman()

运行你的 hangman.py 文件,开始游戏吧!

石头、剪刀、布

石头、剪刀、布游戏使用了 random.choice()、if 语句和获取用户输入与这些函数配合:

  • 用于生成石头、剪刀或布的随机函数。

  • 验证函数用于检查你的动作是否有效

  • 结果函数用于检查谁赢得了这一轮

  • 记分员用于跟踪得分。

代码:

# if not re.match("^[a-z]*$", input_str):
import random
import os
import re

os.system("cls" if os.name == "nt" else "clear")

while 1 < 2:
    print("\n")
    print("Rock, Paper, Scissors - Shoot!")
    userChoice = input("Choose your weapon [R]ock, [P]aper, or [S]cissors, [E]xit: ")
    if userChoice == "E":
        break
    if not re.match("[SsRrPp]", userChoice):
        print("Please choose a letter:")
        print("[R]ock, [S]cissors or [P]paper.")
        continue
    # Echo the user's choice
    print("You chose: " + userChoice)

    choices = ["R", "P", "S"]
    opponenetChoice = random.choice(choices)

    print("I chose: " + opponenetChoice)

    if opponenetChoice == str.upper(userChoice):
        print("Tie!")

    # if opponenetChoice == str("R") and str.upper(userChoice) == "P"

    elif opponenetChoice == "R" and userChoice.upper() == "S":
        print("Scissors beats rock, I win!")
        continue
    elif opponenetChoice == "S" and userChoice.upper() == "P":
        print("Scissors beats paper! I win!")
        continue
    elif opponenetChoice == "P" and userChoice.upper() == "R":
        print("Paper beat rock, I win! ")
        continue
    else:
        print("You win!")

结论

我希望这能帮助你走出舒适区,花 30 分钟来测试你的 Python 技能。我将创建另一篇文章,提供更多来自 YouTuber 的项目教程内容!

尼莎·阿里亚 是一名数据科学家和自由职业技术作家。她特别关注提供数据科学职业建议或教程以及数据科学相关理论知识。她还希望探索人工智能如何及将如何有利于人类寿命的不同方式。她是一名热衷于学习的人员,寻求拓宽自己的技术知识和写作技能,同时帮助指导他人。

更多相关主题

获取免费电子书《伟大的自然语言处理入门》以及《数据科学备忘单全集》和领先的关于数据科学、机器学习、人工智能与分析的新闻通讯,直接发送到您的邮箱。

订阅即表示您接受 KDnuggets 的 隐私政策


<= 上一篇文章下一篇文章 =>

最新文章

|

热门文章

|


© 2024 Guiding Tech Media   |   关于   |   联系   |   广告 |   隐私   |   服务条款

由 Nisha Arya 于 2022 年 10 月 3 日 发布