Create random card shuffle system in Python

 

Introduction

In this tutorial we will learn how to create random card shuffle system in Python using the random.randint(val1,val2). The randint(val1,val2) method returns an integer number selected randomly from the specified range val1,val2. Both the val1 and val2 are included while randomly selecting a value.

Shuffling of cards system includes cards- Spade, Heart, Diamond ,Club and a number range from 1 to 14 where both 1 and 14 is included. Using randint() it generates different combination during each execution of the program.

random module is imported to do actions such as generating random numbers, printing random a value for a list or string.

Code

import random   #import the random module
cards = ['Spade','Heart','Diamond','Club']
print("You got:")
for i in range(4):
  print(random.randint(1,14), "of", cards[i]) #randint()used torange from 1 to 14 where both 1 and 14 is included

Output

You got:
5 of Spade
10 of Heart
8 of Diamond
5 of Club

The output varies during each execution….

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top