samedi 4 décembre 2021

Dealing with two cases when a condition is met?

If the number variable is an integer, an input that is equal to zero should become 'None' and any other value should stay as is. The code below works but for learning purposes I would like to see how this can be achieved differently?

while True:
    try:
        number = int(input('Type an integer: '))  
        if type(number) == int: # If the value is an integer, it proceeds.
            if number == 0: # Here is another 'if' in case the value is zero which has to turn to the 'None' value
                number = None
                break
            else:    # For any other case it should just keep the value from the input above
                break
    except ValueError:
        print("Type an integer")
print(number)

Aucun commentaire:

Enregistrer un commentaire