Issue
I am new at python, so I am making a converter from binary to decimal. I want the user to have the opportunity to enter binary and fro my program to output a number in decimal, however everytime I tried to enter it as a binary it came out as the same binary number instead of a decimal. This is the code I have tried:
print ("Please input your number")
n=int(input())
bin_n = bin(n)
print(int(bin_n, 2))
I think maybe its the form thats being input but idk :/
Solution
If the user is entering the number in binary, use the optional base=2
argument when converting the input, not when printing.
n = int(input("Please input your number: "), 2)
print(n)
If the user enters 1001
it will print 9
.
Answered By - Barmar Answer Checked By - Marilyn (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.