Tôi hiện đang tạo một chương trình Python có liên quan đến byte, nó sẽ nhắc người dùng nhập 2 số nguyên, thêm chúng lại với nhau và nếu nó nằm trong phạm vi kích thước thực tế của một byte [0 đến 255] hiển thị số đó và cũng cho Giá trị tương ứng ở dạng nhị phân 8 bit của nó, ví dụ, tính toán bằng 1, hiển thị số nhị phân mà Python tuyên bố là chính xác.
Đây là nhiệm vụ mà tôi đang làm ~
"Chương trình phải kiểm tra dữ liệu đầu vào cho loại dữ liệu được phép và giới hạn giá trị dữ liệu [giá trị của số nguyên không được vượt quá kích thước thực tế của các số nguyên được mã hóa byte, tức là Min 00000000 và tối đa 11111111 trong cơ sở 2 hoặc tối thiểu 0 và tối đa 255 trong cơ sở 10 cho các số nguyên dương "đầu vào ở cả định dạng nhị phân và thập phân với chuyển đổi sang nhị phân
def add[]:
Num1 = int[input["Input the first number between 0 and 255, the calculated answer must not be above 255: "]]
Num2 = int[input["Input the second number between 0 and 255, the calculated answer must not be above 255: "]]
calculatedanswer = Num1+Num2
if calculatedanswer >= 0 and calculatedanswer 255:
print["Please ensure the added numbers are above 0 and below 255"]
add[]
Đây là mã của tôi cho đến nay, tôi không gặp khó khăn gì khi hiển thị số thập phân tiêu chuẩn, nhưng tôi thực sự không thể có được bin[calculatedanswer]
để hiển thị tương đương nhị phân của nó. Tôi đã thử sử dụng phương pháp này mà tôi tìm thấy trên YouTube.
Tôi nghĩ rằng vấn đề chính của tôi ở đây là sự thiếu hiểu biết của tôi về cách "bin" hoạt động trên Python vì đây thực sự là lần đầu tiên tôi sử dụng nó.
Tôi đã đặt dấu hoa thị xung quanh dòng mà tôi đang gặp rắc rối.
Cải thiện bài viết
Lưu bài viết
Cải thiện bài viết
Lưu bài viết
Đọc
Examples :
Input : 7 Output :111 Input :10 Output :1010
Bàn luận Recursive solution
DecimalToBinary[num]: if num >= 1: DecimalToBinary[num // 2] print num % 2
Được đưa ra một số thập phân làm đầu vào, nhiệm vụ là viết một chương trình Python để chuyển đổi số thập phân đã cho thành số nhị phân tương đương. Các mẫu mực: & nbsp;
Python3
Phương pháp số 1: Giải pháp đệ quy
Dưới đây là việc thực hiện giải pháp đệ quy ở trên: & NBSP;
def
DecimalToBinary[num]:
if
Input : 7 Output :111 Input :10 Output :10100
Input : 7 Output :111 Input :10 Output :10101
Input : 7 Output :111 Input :10 Output :10102
Input : 7 Output :111 Input :10 Output :10103
Input : 7 Output :111 Input :10 Output :10104
Input : 7 Output :111 Input :10 Output :10105
Input : 7 Output :111 Input :10 Output :10106
Input : 7 Output :111 Input :10 Output :10106
Input : 7 Output :111 Input :10 Output :10108
Input : 7 Output :111 Input :10 Output :10109
DecimalToBinary[num]: if num >= 1: DecimalToBinary[num // 2] print num % 21
DecimalToBinary[num]: if num >= 1: DecimalToBinary[num // 2] print num % 22223
Input : 7 Output :111 Input :10 Output :10108
DecimalToBinary[num]: if num >= 1: DecimalToBinary[num // 2] print num % 25
Input : 7 Output :111 Input :10 Output :10101
DecimalToBinary[num]: if num >= 1: DecimalToBinary[num // 2] print num % 27
1001010110001 1010 1010110019
if
DecimalToBinary[num]:
if num >= 1:
DecimalToBinary[num // 2]
print num % 2
9Input : 7
Output :111
Input :10
Output :1010
1Input : 7
Output :111
Input :10
Output :1010
1 1001010110001
1010
101011001
2Input : 7
Output :111
Input :10
Output :1010
3 Decimal to binary using in-built function
Python3
1001010110001 1010 1010110015
Input : 7 Output :111 Input :10 Output :10101
1001010110001 1010 1010110017
Phương pháp số 2: thập phân đến nhị phân bằng cách sử dụng hàm được xây dựng & nbsp;
Input : 7 Output :111 Input :10 Output :10104
Input : 7 Output :111 Input :10 Output :10105
Input : 7 Output :111 Input :10 Output :10106
Input : 7 Output :111 Input :10 Output :10106
Input : 7 Output :111 Input :10 Output :10108
Input : 7 Output :111 Input :10 Output :10109
DecimalToBinary[num]: if num >= 1: DecimalToBinary[num // 2] print num % 21
bin[calculatedanswer]
6bin[calculatedanswer]
7bin[calculatedanswer]
8
DecimalToBinary[num]: if num >= 1: DecimalToBinary[num // 2] print num % 21
bin[calculatedanswer]
6def
2bin[calculatedanswer]
8
DecimalToBinary[num]: if num >= 1: DecimalToBinary[num // 2] print num % 21
DecimalToBinary[num]: if num >= 1: DecimalToBinary[num // 2] print num % 22223
Input : 7 Output :111 Input :10 Output :10108
DecimalToBinary[num]: if num >= 1: DecimalToBinary[num // 2] print num % 25
Input : 7 Output :111 Input :10 Output :10101
DecimalToBinary[num]: if num >= 1: DecimalToBinary[num // 2] print num % 27
if
DecimalToBinary[num]:
if num >= 1:
DecimalToBinary[num // 2]
print num % 2
9Input : 7
Output :111
Input :10
Output :1010
1Input : 7
Output :111
Input :10
Output :1010
1 1001010110001
1010
101011001
2Input : 7
Output :111
Input :10
Output :1010
3Without in-built function
Python3
1001010110001 1010 1010110015
Input : 7 Output :111 Input :10 Output :10101
1001010110001 1010 1010110017
Phương pháp số 2: thập phân đến nhị phân bằng cách sử dụng hàm được xây dựng & nbsp;
Input : 7 Output :111 Input :10 Output :10104
Input : 7 Output :111 Input :10 Output :10105
Input : 7 Output :111 Input :10 Output :10106
Input : 7 Output :111 Input :10 Output :10106
Input : 7 Output :111 Input :10 Output :10108
Input : 7 Output :111 Input :10 Output :10109
DecimalToBinary[num]: if num >= 1: DecimalToBinary[num // 2] print num % 21
bin[calculatedanswer]
6bin[calculatedanswer]
7bin[calculatedanswer]
8
DecimalToBinary[num]: if num >= 1: DecimalToBinary[num // 2] print num % 21
bin[calculatedanswer]
6def
2bin[calculatedanswer]
8
DecimalToBinary[num]: if num >= 1: DecimalToBinary[num // 2] print num % 21
bin[calculatedanswer]
6def
7bin[calculatedanswer]
8
DecimalToBinary[num]:
if num >= 1:
DecimalToBinary[num // 2]
print num % 2
1DecimalToBinary[num]:
if num >= 1:
DecimalToBinary[num // 2]
print num % 2
2223 Input : 7
Output :111
Input :10
Output :1010
8DecimalToBinary[num]:
if num >= 1:
DecimalToBinary[num // 2]
print num % 2
5Input : 7
Output :111
Input :10
Output :1010
1 DecimalToBinary[num]:
if num >= 1:
DecimalToBinary[num // 2]
print num % 2
7 One Line Code to Convert
Decimal to Binary with user input
Python3
or
Python3
if
DecimalToBinary[num]: if num >= 1: DecimalToBinary[num // 2] print num % 29
Input : 7 Output :111 Input :10 Output :10101
Input : 7 Output :111 Input :10 Output :10101
1001010110001 1010 1010110012
Input : 7 Output :111 Input :10 Output :10103
DecimalToBinary[num]: if num >= 1: DecimalToBinary[num // 2] print num % 21
DecimalToBinary[num]:
6The binary of the number 60 is 1111004
Input : 7 Output :111 Input :10 Output :101006
Input : 7 Output :111 Input :10 Output :10108
Input : 7 Output :111 Input :10 Output :101008
1001010110001 1010 1010110015
Input : 7 Output :111 Input :10 Output :10101
1001010110001 1010 1010110017
DecimalToBinary[num]: if num >= 1: DecimalToBinary[num // 2] print num % 21
DecimalToBinary[num]:
6The binary of the number 60 is 1111004
Input : 7 Output :111 Input :10 Output :101015
Input : 7 Output :111 Input :10 Output :10108
Input : 7 Output :111 Input :10 Output :101008
Phương pháp số 2: thập phân đến nhị phân bằng cách sử dụng hàm được xây dựng & nbsp;
DecimalToBinary[num]: if num >= 1: DecimalToBinary[num // 2] print num % 21
DecimalToBinary[num]:
6The binary of the number 60 is 1111004
Input : 7 Output :111 Input :10 Output :101024
Input : 7 Output :111 Input :10 Output :10108
Input : 7 Output :111 Input :10 Output :101008
Đầu ra
1001010110001 1010 101011001
Sử dụng toán tử Bitwise Shift >>.
Python3
def
Input : 7 Output :111 Input :10 Output :101028
DecimalToBinary[num]:
77Input : 7 Output :111 Input :10 Output :101030
Input : 7 Output :111 Input :10 Output :101032
Input : 7 Output :111 Input :10 Output :10101
Input : 7 Output :111 Input :10 Output :101034
if
Input : 7 Output :111 Input :10 Output :101037
Input : 7 Output :111 Input :10 Output :10101
Input : 7 Output :111 Input :10 Output :10101
Input : 7 Output :111 Input :10 Output :101040
Input : 7 Output :111 Input :10 Output :101030
Input : 7 Output :111 Input :10 Output :10104
The binary of the number 60 is 1111003
Input : 7 Output :111 Input :10 Output :101040
Input : 7 Output :111 Input :10 Output :101046
Input : 7 Output :111 Input :10 Output :101047
Input : 7 Output :111 Input :10 Output :10104
Input : 7 Output :111 Input :10 Output :101032
Input : 7 Output :111 Input :10 Output :101050
Input : 7 Output :111 Input :10 Output :10101
Input : 7 Output :111 Input :10 Output :101052
Input : 7 Output :111 Input :10 Output :101053__12
Input : 7 Output :111 Input :10 Output :10104
Input : 7 Output :111 Input :10 Output :101057
Input : 7 Output :111 Input :10 Output :10101
Input : 7 Output :111 Input :10 Output :101059
Input : 7 Output :111 Input :10 Output :10102
Input : 7 Output :111 Input :10 Output :101032
Input : 7 Output :111 Input :10 Output :10101
Input : 7 Output :111 Input :10 Output :101064
Input : 7 Output :111 Input :10 Output :101065__12
The binary of the number 60 is 1111003
Input : 7 Output :111 Input :10 Output :101070
def
Input : 7 Output :111 Input :10 Output :101072
Input : 7 Output :111 Input :10 Output :101057
Input : 7 Output :111 Input :10 Output :10101
Input : 7 Output :111 Input :10 Output :101076
DecimalToBinary[num]: if num >= 1: DecimalToBinary[num // 2] print num % 21
Input : 7 Output :111 Input :10 Output :101079
Input : 7 Output :111 Input :10 Output :101080
Input : 7 Output :111 Input :10 Output :10109
if
DecimalToBinary[num]: if num >= 1: DecimalToBinary[num // 2] print num % 29
Input : 7 Output :111 Input :10 Output :10101
Input : 7 Output :111 Input :10 Output :10101
Input : 7 Output :111 Input :10 Output :101086
Input : 7 Output :111 Input :10 Output :10103
Input : 7 Output :111 Input :10 Output :101089
Đầu ra
The binary of the number 60 is 111100