mercredi 25 mars 2020

whilst using 3 if statements in a row, if the third one equals to true it does nothing somehow?

i started learning programming and my first language is python. i got a excersise in where i need to return a certain string based on the number i input. but if multiple if's equal to true it has to concatenate the strings and print the answer to the console. here's the code i created:

number = int(input("enter your number here: "))
def plingplangplong(number):
    strPling = "pling"
    strPlong = "plong"
    strPlang = "plang"
    strAnswer = ""
    if number % 3 == 0:
        strAnswer = strAnswer + strPling
    if number % 5 == 0:
        strAnswer = strAnswer + strPlong
    if number % 7 == 0:
        strAnswer = strAnswer + strPlang
    elif strAnswer:
        print(strAnswer)
    else:
        print(number)


plingplangplong(number)

it seems like it should work to me, but as soon as i enter a number that you can cleanly divide by 7 it does not give any output. so numbers like 7,28,35 don't work. 3,5 do work, 30 and 60 does work and returns 'plingplong' as expected.

what do i need to change? and more tips would be usefull since im a filthy beginner :)

Aucun commentaire:

Enregistrer un commentaire