How To Create A Python Function Program

Photo by Christopher Gower on Unsplash








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 can then create comments in Python.



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


After the comment, I would usually import the functions 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.


x=1

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

Great Ways To Learn How To Start Coding

Good Places To Get Help When Learning How To Code There are many places to learn how to do many things not only code, and today in this a...