dimanche 8 août 2021

Recieving SyntaxError from IF ELSE in python

So, I've been messin' round in Jupyter until I got this error:

  File "<ipython-input-7-c712510e86db>", line 58
    else:
    ^
SyntaxError: invalid syntax

Here is the source code for the whole class and methods:

class RandomGenerator:
    def __init__(self):
        pass
    
def random_int(self, start, stop, dtype='int', length=1):
    #Returns random integer "to" dtype
        
    if length == 1 and dtype in 'int float':
        if dtype == 'int':
            return rd.randint(start, stop)
        elif dtype == 'float':
            raise TypeError('Cannot make random integer float. Check out the random_float() function for floats.')
    elif length > 1 and dtype in 'int float':
        raise TypeError(f'Datatype is {dtype} and length is {length} while length should be 1.')
    elif length >= 1 and not dtype in 'int float':
        if dtype == 'list':
            return list([rd.randint(start, stop) for n in range(length)])
        elif dtype == 'tuple':
            return tuple([rd.randint(start, stop) for n in range(length)]) 
    else:
        raise TypeError(f'Invalid DataType {dtype}.')
                
def random_float(self, start, stop, dtype='float', length=1):
    #Returns random float "to" dtype
        
    if length == 1 and dtype in 'int float':
        if dtype == 'int':
            raise TypeError('Cannot make random float integer. Check out the random_int() function for ints.')
        elif dtype == 'float':
            return rd.uniform(start, stop)
    elif length > 1 and dtype in 'int float':
        raise TypeError(f'Datatype is {dtype} and length is {length} while length should be 1.')
    elif length >= 1 and not dtype in 'int float':
        if dtype == 'list':
            return list([rd.uniform(start, stop) for n in range(length)])
        elif dtype == 'tuple':
            return tuple([rd.uniform(start, stop) for n in range(length)]) 
    else:
        raise TypeError(f'Invalid DataType {dtype}.')

    def random_float_onezero(self, dtype='float', length=1):
        #Returns random float between 0 and 1
        
        if length == 1 and dtype in 'int float':
            if dtype == 'int':
                raise TypeError('Cannot make random float integer.')
            elif dtype == 'float':
                return rd.random()
        elif length > 1 and dtype in 'int float':
            raise TypeError(f'Datatype is {dtype} and length is {length} while length should be 1.')
        elif length >= 1 and not dtype in 'int float':
            if dtype == 'list':
                return list([rd.random() for n in range(length)])
            elif dtype == 'tuple':
                return tuple([rd.random() for n in range(length)]
            else:
                raise TypeError(f'Invalid DataType {dtype}.')

Well, the if, elif and else are correctly aligned, and i don't know what to do. So, if you can, please, answer me. I really appreciate :]

Aucun commentaire:

Enregistrer un commentaire