【发布时间】:2018-05-09 02:00:12
【问题描述】:
我正在制作一个将二进制转换为八进制、十进制和十六进制值的程序。如何检查用户的输入值是否为二进制数? 代码如下:
def repeat1():
if choice == 'B' or choice == 'b':
x = input("Go on and enter a binary number: ")
y = int(x, 2)
print(x, "in octal is", oct(y))
print(x, "in decimal is", y)
print(x, "in hexidecimal is", hex(y))
print(" ")
def tryagain1():
print("Type '1' to convert from the same number base")
print("Type '2' to convert from a different number base")
print("Type '3' to stop")
r = input("Would you like to try again?")
if r == '1':
repeat1()
elif r == '2':
loop()
elif r == '3':
print("Thank you for using the BraCaLdOmbayNo Calculator!")
else:
print("You didn't enter any of the choices! Try again!")
tryagain1()
tryagain1()
提前谢谢你!
【问题讨论】:
-
不要发布链接或图片。剪切并粘贴问题中的实际代码,以便人们可以执行相同的操作来运行您的代码和/或更改它以获得答案。请参阅How to Ask 以及如何创建minimal reproducible example。
-
二进制数必须由 0 和 1 组成。从这里开始,由您决定。如果你说这个数字是二进制的,它就是二进制的。例如 101101011 由 0 和 1 组成,但它可以是十进制、十六进制、二进制或其他。
-
@MarkTolonen,谢谢你,会改进的。