Hướng dẫn what is guess python?

Skip to content

  • Career Tracks
  • Courses
  • Blog
  • Reviews
  • Login
  • Start for free
  • Career Tracks
    • Java Career Track
    • Python Career Track
    • Data Science Career Track
    • Job Support Program
  • Courses
    • Java Programming
    • Python Programming
    • Data Science + Machine Learning
    • Django Web Development
    • Flask Web Development
    • Intro to JavaScript
    • SQL & DatabasesFree
    • Python APIs & DatabasesFree
    • Git & GitHubFree
    • Spring FrameworkComing Soon
    • Upwork Freelancing Mastery
  • Blog
  • Reviews
  • Log In
  • Start For Free

Nội dung chính

  • Python Project for Beginners: Guess-the-Number Game
  • Introduction
  • Video Tutorial: Python project for beginners
  • Install Python on your computer
  • Build the Game
  • Coding in your text editor
  • Save your code!
  • Play the Game
  • Run your Python project file
  • Parts of a Python project
  • Tutorial take-aways
  • What is guess Python?
  • How do you play guessing in Python?
  • How do you randomly guess a word in Python?
  • What is a number guessing game?

Python Project for Beginners: Guess-the-Number Game

Python Project for Beginners: Guess-the-Number Game

Introduction

In this article and video, you will learn how to write a simple Guess-the-number game in Python using a normal text editor.

This tutorial is meant to be an easy Python project for beginners, so don’t worry if you don’t understand everything at first. The main point is to see that code is just text.

You can watch the video tutorial below, and/or continue reading this blog for the written tutorial. Now let’s go build and run your first interactive Python project!

  • Video Tutorial: Python project for beginners
  • Install Python on your computer
  • Build the Game
    • Coding in your text editor
    • Save your code!
  • Play the Game
    • Run your Python project file
  • Parts of a Python project
  • Tutorial take-aways

Video Tutorial: Python project for beginners

Install Python on your computer

If you don’t already have Python installed on your computer, visit Python.org to download it. This tutorial is taught for UNIX-based systems (Mac & Linux). If you are on Windows and you don’t have Python pre-installed, you can also build the game in an online coding interface, such as repl.it.

Build the Game

The first step in building your Python project for beginners game is to write your code in a text editor.

Coding in your text editor

Open up any Text Editor – this can be as simple as the built-in TextEdit program on MacOS:

Code is just plain text. So, if you’re using TextEdit, you can press Cmd+Shift+t to switch to plain text. Doing so means that you can’t apply any formatting, such as bold or italics. Remember that code is just text, so you won’t need any formatting for it. Your window should look like this now:

Now it’s time to write some code! Type the following code into your TextEdit file:

import random

num = random.randint(1, 10)
guess = None

while guess != num:
    guess = input("guess a number between 1 and 10: ")
    guess = int(guess)

    if guess == num:
        print("congratulations! you won!")
        break
    else:
        print("nope, sorry. try again!")

Make sure you type the code exactly as you see it above, including the 4 spaces for indentation. You can also copy-paste the code from this online resource. Your text window should look like this, which is already the full code for this Python project for beginners:

Save your code!

Finally, let’s save your text file using the python file extension .py.

Press Cmd+s or go to File/Save and save it on your Desktop with the name guess.py:

And that’s it for writing the code. Next step is to run the code and play your game.

Play the Game

Well done so far! 🙂 To play your game on your computer you need to run the Python file you just created.

Run your Python project file

To run your Python project on MacOS, open up your Terminal. Press Cmd+Space to open up Spotlight, and type Terminal, then press Enter:

This will open up your Terminal, a tool that programmers use on a daily basis. If you join one of our courses, you will get to know your Terminal in much more detail, but for this beginner project you don’t need to worry about it too much. Just type the following in there:

cd ~/Desktop

This will teleport you to your Desktop, where you saved the guess.py file that contains your code-text.

Now you finally get to play your guess-the-number game. Since you wrote it in Python, you need to also start it using Python. In your terminal, type the following and press Enter:

python guess.py

And lo and behold! Here you are! You’ve officially built and run your very own Python project for beginners game! Now you are ready to play.

If you want to play again after it finished, you can press the up arrow once and your terminal will show you the previous command:

python guess.py

Pressing Enter will start the program again from the beginning.

Have fun guessing the number! 😀

Parts of a Python project

There are a lot of different concepts that went into creating even this simple Python project for beginners. Let’s take a look what they are:

In the screenshot above you can see the filename of the Python file you created, as well as the code saved you saved in the file. Now let’s dive deeper into the code and learn about which programming concepts you touched upon by making this file.

Fair warning: There’s a lot going on! Much like a paragraph of English, a script can be broken into many parts: an introductory sentence, references to outside text, subjects, nouns, verbs, and sometimes even new vocabulary.

Keep in mind that, like a paragraph of English, if you understand these parts, then it’s likely that you can write and understand another similar paragraph. Even if you haven’t seen it before. It also takes some training before you will be able to do that, so don’t feel overwhelmed if you won’t grasp it all right away. For now enjoy this depiction of the cool code you wrote! 🙂

We know that is a lot of parts with a lot of colors! If you’re intrigued to learn more about writing your own Python projects, check out CodingNomads Python Programming course. With some time and effort, you will soon be able to grasp these concepts and start speaking the coding language too!

Tutorial take-aways

  • Code is just Text: Programming is just writing text, and all you need is a simple text editor to get started!
  • Run using Python: After writing your code text, you run Python programs with python
  • Python project components: Yes, code is just text… but is it also code 🙂 There are many parts of a Python project, but once you get a grasp you open the door to creating any Python project of your own.

Congratulations again on building your first Python project for beginners! If you’re interested to learn more Python coding, click below to check out CodingNomads’ Python Programming Course, and Python Career Track Course.

Martin Breuss2020-11-12T15:12:05-08:00

Start Today For Free!

Want to speak with a mentor?

Got questions? We’ve got answers! Feel free to schedule a quick 15 minute AMA with a CodingNomads instructor at a time that works best for you!  Click here to schedule your call

Contact Info

Let’s be friends

This website uses cookies to improve your experience. By continuing to browse our site you agree to our Cookie Policy.
AcceptReject

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.

Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.

Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.

What is guess Python?

This script is an interactive guessing game, which will ask the user to guess a number between 1 and 99. We are using the random module with the randint function to get a random number. The script also contains a while loop, which make the script run until the user guess the right number.

How do you play guessing in Python?

Coding in your text editor import random num = random. randint(1, 10) guess = None while guess != num: guess = input("guess a number between 1 and 10: ") guess = int(guess) if guess == num: print("congratulations! you won!

How do you randomly guess a word in Python?

choice() function will choose one random word from the gven list of words. word1 = rdm. choice(words1) print ("Please guess the characters: ")

What is a number guessing game?

A number guessing game is a simple guessing game where a user is supposed to guess a number between 0 and N in a maximum of 10 attempts. The game will end after 10 attempts and if the player failed to guess the number, and then he loses the game.