简体   繁体   English

在 python 中打印不需要的符号

[英]Printing an unwanted symbol in python

I have to make a python script for a school project that converts decimal numbers to binary numbers.我必须为将十进制数转换为二进制数的学校项目制作 python 脚本。 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%例如,当我在开头输入128而不是得到10000000结果时,我得到10000000%

I am currently using python 3.7.4我目前正在使用 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:和 output 将是: 0b10000000 然后你可以简单地省略 0b 像:

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])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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