Python Function Program

How to create a python program using the random function

This is a Python 3 tutorial for creating a random function program, and in order to get started I always start with a comment about what the program that I am building is. By using the # key you are able to then create comments in Python.

# This is a simple guessing game program using the random function

After the comment, I would usually then import the functions that I would utilize within the program. In this programming case, I will be utilizing the random function.

import random

Next, I will initialize our variable x to 1 then follow that up with a while loop.


The while loop continues to execute as long as the value of x is equal to 1. Remember that in python indentation is important everything line must be aligned correctly, or you will receive a syntax error.

while x == 1:      
              number = int(input("Can you guess which number I am thinking of? "))
              guessing = random.randint(1, 25)
              if number == guessing:
                             print("You got it! Wow you are a good guesser") 

In this next example, I also utilize the python random function to create a guessing game by using the Random.choice function.

# this is a simple guessing program

import random

number = int(input("Can you guess which number I am thinking of? "))

x = [1,2,3,4,5,6,7,8,9]
guessing = random.choice(x)

Both examples can be seen in the YouTube tutorial that I have below.

No comments:

Post a Comment

Best Devices To Get Started With Programming

Having a good solid computer is vital to start coding, and HP offers solid laptops for coders at every level.