code:
# -*- coding: utf-8 -*-
"""
Created on Sat Nov 18 19:40:26 2023
@author: SavingKing
"""
s1= input("請輸入第一個二進位數字:\t")
s2= input("請輸入第二個二進位數字:\t")
s1_0b = "0b"+s1
s2_0b = "0b"+s2
int1 = int(s1_0b,2)
int2 = int(s2_0b,2)
sum1 = int1+int2
print(f"{int1} + {int2} = {sum1}")
print(f"{sum1:08b}")
#因為題目的輸出剛好8位數的第一位數都是1
#也可使用bin(),會省略前導0 ,
#但以f string解題為佳
輸出結果:
推薦hahow線上學習python: https://igrape.net/30afN