Using f-string for formatted string output

Pitfall avoidance record

When using f-string in front of a string

Don't use %, otherwise it will be treated as a placeholder. You should use it directly.Variable name: Just output format

Learn to test code

'''
小明的成绩从去年的72分提升到了今年的85分,
请计算小明成绩提升的百分点,并用字符串格式化显示出'xx.x%',
只保留小数点后1位:
'''


def practice():
    last_score = 72
    now_score = 85

    reslut = (now_score - last_score) / last_score * 100
    string = f'从去年到现在,小明的成绩从{
      
      last_score}提高到了{
      
      now_score}。小明的成绩提高了{
      
      reslut:.1f}%'
    print(string)


if __name__ == '__main__':
    practice()

Guess you like

Origin blog.csdn.net/weixin_44943389/article/details/132979809