简体   繁体   中英

Printing an unwanted symbol in python

I have to make a python script for a school project that converts decimal numbers to binary numbers. I was able to write the code but every time when executed it prints the binary number and the "%" symbol at the end.
How can I remove the "%" symbol from when it is printing it?

This is the script:

first=int(input("input a decimal number to convert to a binary: "))
toBase=2
first2=first
char=0
char2=0
char3=0

while first2!=0:
    first2//=2
    char+=1

binNum=[0]*char

while first!=0:
   rem=first%2
   first//=2
   binNum[char2]=rem
   char2+=1

for loop in range(char):
   char3-=1
   print(binNum[char3],end="")

As an example, when I enter 128 in the beginning instead of getting 10000000 as a result I get 10000000%

I am currently using python 3.7.4

I just copied your code and run it. it was Ok and the problem you mentioned didn't happen.

But one more thing: you can use this method:

bin(128)

and the output will be: 0b10000000 then you can simply omit 0b like:

a=bin(128)
print(a[2:])

The Qutput

It can be done like below:

deci_num = int(input("input a decimal number to convert to a binary: "))
bin = ''
while(deci_num):
     if(deci_num % 2 == 1):
          bin += '1'
     else:
          bin += '0'
     deci_num //= 2
print (bin[::-1])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM