Nairda123
Nairda123

Reputation: 313

How to convert input to a random type? Python

I am trying to write a function that takes inputs and converts them into random types.

e.g. input as integer and output would be random of int,str,bool or float

Upvotes: 0

Views: 135

Answers (1)

bglbrt
bglbrt

Reputation: 2098

I don't know if this is what you're looking for... as I don't see the point of doing that but I guess that this works:

import random

def random_type(x):
    fun = random.choice([int, float, str, bool])
    return fun(x)

Upvotes: 1

Related Questions