PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, August 7, 2022

[FIXED] How do I get a user to enter a binary number and turn it into decimal?

 August 07, 2022     binary, decimal, python     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing