Add this in the position in PS1
where you want the exit code to appear:
%(?..[%?] )
It's a conditional expression. The part between the two dots (nothing in this case) is output if the expression before the first dot is true. The part after the second dot is output if it's false.
For example:
PS1='%n%m %~ %(?..[%?] )%# '
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…