print random symbols in loop in python

To print random symbols in a loop in Python, you can use the random module along with a loop to generate and print the symbols. Here's an example code block:

main.py
import random

symbols = ['*', '$', '&', '#', '@']
num_symbols = 5

for i in range(num_symbols):
    symbol = random.choice(symbols)
    print(symbol, end=' ')
160 chars
9 lines

In this code, we first import the random module. Then, we define a list of symbols and the number of symbols we want to print.

Inside the for loop, we use the random.choice() function to randomly select a symbol from the list of symbols. Then, we print the chosen symbol using the print() function. The end parameter is used to print a space after each symbol instead of a newline character.

When you run this code block, it will print num_symbols random symbols from the symbols list on the same line.

gistlibby LogSnag