Home > Blockchain >  printing variables in an f-string with conditions
printing variables in an f-string with conditions

Time:05-25

can't seem to print the variables inside the f-string with conditions. I'm running this on flask hence the flash. Tried removing the braces and interchanging the quotes from single to double but still doesn't work. The output should be values of the variables and not the literal variable names ex. 50 instead of {driving_num}.

flash(f"{'The mandatory penalty for driving at {driving_num} km/h in a {speed_num} km/h zone is {msg[1]}' if msg[0] else 'The discretional penalty for driving at {driving_speed} km/h in a {speed_limit}km/h zone'} points.", WARNING_MSG)

CodePudding user response:

Your inner strings need to be f-strings, too. Ignore the indentation which is just for slightly better readability:

flash(
    f"{
        f'The mandatory penalty for driving at {driving_num} km/h in a {speed_num} km/h zone is {msg[1]}'
        if msg[0] else
        f'The discretional penalty for driving at {driving_speed} km/h in a {speed_limit}km/h zone'
    } points.",
    WARNING_MSG
)
  • Related