Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python. Generating a random + or - sign using the random command

What is the easiest way to generate a random +,-,*, or / sign using the import random function while assigning this to a letter.

E.G.

g = answeryougive('+','-')

Thanks in advance :)

like image 635
user1974907 Avatar asked Jul 14 '26 10:07

user1974907


1 Answers

You want random.choice

random.choice(['+', '-'])

Or more concisely:

random.choice('+-')
like image 143
Eric Avatar answered Jul 17 '26 18:07

Eric