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  Microsoft Build 2019 | Serverless Python Applications with Azure Functions

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  Cloud Foundry Ecosystem Coalesces Around Kubernetes With Significant Project Updates Announced By Cloud Foundry Foundation

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
  • 1
    Enterprises are keen on cloud repatriation – but not for all workloads
    • June 4, 2025
  • 2
    The Summer Adventures : Hiking and Nature Walks Essentials
    • June 2, 2025
  • 3
    Just make it scale: An Aurora DSQL story
    • May 29, 2025
  • 4
    Reliance on US tech providers is making IT leaders skittish
    • May 28, 2025
  • Examine the 4 types of edge computing, with examples
    • May 28, 2025
  • AI and private cloud: 2 lessons from Dell Tech World 2025
    • May 28, 2025
  • 7
    TD Synnex named as UK distributor for Cohesity
    • May 28, 2025
  • Weigh these 6 enterprise advantages of storage as a service
    • May 28, 2025
  • 9
    Broadcom’s ‘harsh’ VMware contracts are costing customers up to 1,500% more
    • May 28, 2025
  • 10
    Pulsant targets partner diversity with new IaaS solution
    • May 23, 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
  • Understand how Windows Server 2025 PAYG licensing works
    • May 20, 2025
  • By the numbers: How upskilling fills the IT skills gap
    • May 21, 2025
  • 3
    Cloud adoption isn’t all it’s cut out to be as enterprises report growing dissatisfaction
    • May 15, 2025
  • 4
    Hybrid cloud is complicated – Red Hat’s new AI assistant wants to solve that
    • May 20, 2025
  • 5
    Google is getting serious on cloud sovereignty
    • May 22, 2025
  • /
  • Technology
  • Tools
  • About
  • Contact Us

Input your search keywords and press Enter.