jeudi 11 octobre 2018

Depending values in an instance of a class

I am new to python and stuck with a simple problem: Here is an example code:

import random
class shirt:
    def __init__(self, color, size, size_description):
        self.color = color
        self.size = size
        self.size_description = size_description

def randomshirtsize():
    randomshirtsize_ch = (
        's',
        'm',
        'l'
    )
    return random.choice(randomshirtsize_ch)

def randomshirtdescription():
    if randomshirtsize() == 's':
        return 'small'
    elif randomshirtsize() == 'm':
        return 'medium'
    elif randomshirtsize() == 'l':
        return 'large'
    else:
        return 'Error! No valid size found'

shirt1 = shirt('blue', randomshirtsize(), randomshirtdescription())

print('Your shirt is size ' + shirt1.size + '. In other words ' + shirt1.size_description)

In theory, randomshirtdescription() should match the output of randomshirtsize(), but it is totally random. Can anyone help me out? I also want to stay with my classes/instances approach to this, because my main code is much larger and depends on classes and instances.

Thanks in advance!




Aucun commentaire:

Enregistrer un commentaire