aster.cloud aster.cloud
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
    • Learning
  • Tools
  • About
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
    • Learning
  • Tools
  • About
aster.cloud aster.cloud
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
    • Learning
  • Tools
  • About
  • Programming
  • Software

Learn Python By Coding A Simple Game

  • root
  • January 24, 2021
  • 3 minute read

Explore Python (and other programming languages) by writing a “guess the number” game.

In this series, we’re writing the same application in different programming languages to compare how various languages work and to show how using a standard test program is a great way to learn new ways to program.


Partner with aster.cloud
for your next big idea.
Let us know here.



From our partners:

CITI.IO :: Business. Institutions. Society. Global Political Economy.
CYBERPOGO.COM :: For the Arts, Sciences, and Technology.
DADAHACKS.COM :: Parenting For The Rest Of Us.
ZEDISTA.COM :: Entertainment. Sports. Culture. Escape.
TAKUMAKU.COM :: For The Hearth And Home.
ASTER.CLOUD :: From The Cloud And Beyond.
LIWAIWAI.COM :: Intelligence, Inside and Outside.
GLOBALCLOUDPLATFORMS.COM :: For The World's Computing Needs.
FIREGULAMAN.COM :: For The Fire In The Belly Of The Coder.
ASTERCASTER.COM :: Supra Astra. Beyond The Stars.
BARTDAY.COM :: Prosperity For Everyone.

When you learn a new programming language, it’s good to focus on the things they have in common. Variables, expressions, and statements are the basis of most programming languages. Once you understand these concepts, you can start figuring the rest out.

Because programming languages share many similarities, once you know one, you can usually learn the basics of another by looking at how it differs from what you know. Using a standard test program you’ve written in other languages enables you to focus on the language, not the program’s logic.

 

To prove this point, we’re testing how to write a “guess the number” program in multiple languages. The computer picks a number between one and 100 and asks you to guess it. The program loops until you guess the correct answer.

The “guess the number” program exercises several concepts in programming languages:

  • Variables
  • Input
  • Output
  • Conditional evaluation
  • Loops

It’s a great practical experiment to learn a new programming language.

 

Guess the number in Python

In the words of the Python Software Foundation: “Python is an interpreted, interactive, object-oriented programming language. It incorporates modules, exceptions, dynamic typing, very high-level dynamic data types, and classes.” It’s a great general-purpose programming language that’s useful for everything from simple scripts to complex GUI applications.

Read More  Our I/O 2022 Announcements: In Demo Form

You can explore Python by writing a version of the “guess the number” game. Here is my implementation:

To assign a value to a variable, list the variable’s name, followed by the = sign. For example, the statement random = 0 assigns a zero value to the random variable.

The first line of the script imports the random module. Since all the programs in this series use random as the name of the variable, you can use import random as randomlib to import it under an alias to avoid name conflicts.

Few functions are built into Python; most have to be imported explicitly from the standard library. The random standard library module has the functionality to generate all kinds of random values.

The second line of the script reads the result of the function randint() and assigns it to the variable called random. The function takes two arguments: a minimum and a maximum. In this case, the range is 1 to 100, making the game just challenging enough.

You can also prompt the user to enter a value using the input() function. If you write guess = int(input()), Python waits for the user to enter some text, converts it to an integer, and then stores that value in the guess variable.

Python supports conditional expressions and flow control like loops. In the “guess the number” game, Python continues looping as long as the value in guess is not equal to random.

If the guess is less than the random number, Python prints Too low, and if the guess is greater than the number, Python prints Too high.

 

Read More  3 New Ways To Authorize Users To Your Private Workloads On Cloud Run

Sample output

Now that you’ve written your Python program, run it to play the “guess the number” game. Every time you run the program, Python will pick a different random number. To complete this game, you need to guess until you find the correct number:

This “guess the number” game is a great introductory program when learning a new programming language because it exercises several common programming concepts in a pretty straightforward way. By implementing this simple game in different programming languages, you can demonstrate some core concepts of different programming languages and compare each language’s details.

Do you have a favorite programming language? How would you write the “guess the number” game? Follow this article series to see examples of other programming languages that might interest you!

This feature is originally appeared in opensource.com

For enquiries, product placements, sponsorships, and collaborations, connect with us at [email protected]. We'd love to hear from you!

Our humans need coffee too! Your support is highly appreciated, thank you!

root

Related Topics
  • programming languages
  • Python
  • Python software
You May Also Like
View Post
  • Software
  • Technology

Canonical Releases Ubuntu 25.04 Plucky Puffin

  • April 17, 2025
View Post
  • Software
  • Technology

IBM Accelerates Momentum in the as a Service Space with Growing Portfolio of Tools Simplifying Infrastructure Management

  • March 27, 2025
Vehicle manufacturing
View Post
  • Software

IBM Study: Vehicles Believed to be Software Defined and AI Powered by 2035

  • December 12, 2024
aster-cloud-tux-gaming
View Post
  • Computing
  • Gears
  • Software

5 best Linux distributions for gamers in 2024

  • September 11, 2024
Crab
View Post
  • Gears
  • Learning
  • Software

The Best Friends for a Rustacean. Top Books in Learning Rust.

  • August 25, 2024
Coffee | Laptop | Notebook | Work
View Post
  • Software

The Hidden Economy Of Open Source Software

  • April 28, 2024
Redis logo
View Post
  • Platforms
  • Software

Redis Moves To Source-Available Licenses

  • April 2, 2024
View Post
  • Software
  • Technology

Charmed MongoDB Enters General Availability

  • March 26, 2024

Stay Connected!
LATEST
  • college-of-cardinals-2025 1
    The Definitive Who’s Who of the 2025 Papal Conclave
    • May 7, 2025
  • conclave-poster-black-smoke 2
    The World Is Revalidating Itself
    • May 6, 2025
  • oracle-ibm 3
    IBM and Oracle Expand Partnership to Advance Agentic AI and Hybrid Cloud
    • May 6, 2025
  • 4
    Conclave: How A New Pope Is Chosen
    • April 25, 2025
  • Getting things done makes her feel amazing 5
    Nurturing Minds in the Digital Revolution
    • April 25, 2025
  • 6
    AI is automating our jobs – but values need to change if we are to be liberated by it
    • April 17, 2025
  • 7
    Canonical Releases Ubuntu 25.04 Plucky Puffin
    • April 17, 2025
  • 8
    United States Army Enterprise Cloud Management Agency Expands its Oracle Defense Cloud Services
    • April 15, 2025
  • 9
    Tokyo Electron and IBM Renew Collaboration for Advanced Semiconductor Technology
    • April 2, 2025
  • 10
    IBM Accelerates Momentum in the as a Service Space with Growing Portfolio of Tools Simplifying Infrastructure Management
    • March 27, 2025
about
Hello World!

We are aster.cloud. We’re created by programmers for programmers.

Our site aims to provide guides, programming tips, reviews, and interesting materials for tech people and those who want to learn in general.

We would like to hear from you.

If you have any feedback, enquiries, or sponsorship request, kindly reach out to us at:

[email protected]
Most Popular
  • 1
    Tariffs, Trump, and Other Things That Start With T – They’re Not The Problem, It’s How We Use Them
    • March 25, 2025
  • 2
    IBM contributes key open-source projects to Linux Foundation to advance AI community participation
    • March 22, 2025
  • 3
    Co-op mode: New partners driving the future of gaming with AI
    • March 22, 2025
  • 4
    Mitsubishi Motors Canada Launches AI-Powered “Intelligent Companion” to Transform the 2025 Outlander Buying Experience
    • March 10, 2025
  • PiPiPi 5
    The Unexpected Pi-Fect Deals This March 14
    • March 13, 2025
  • /
  • Technology
  • Tools
  • About
  • Contact Us

Input your search keywords and press Enter.