user1974907
user1974907

Reputation: 41

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 :)

Upvotes: 3

Views: 6738

Answers (2)

Ben
Ben

Reputation: 1

Thanks i am doing my GCSE great help

there are several ways to do this but the best is using random.choice for random symbols or random.radiant for numbers Eg.

import random
Num1=random.radiant(1,12) #random number one
Num2=random.radiant(1.12) #random number two
sym=random.choice(['+','/','*'.'-']) #random symbol

Upvotes: -2

Eric
Eric

Reputation: 97631

You want random.choice

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

Or more concisely:

random.choice('+-')

Upvotes: 12

Related Questions