'x' 未在自定义十进制转换函数中定义
'x' is not defined in custom decimal conversion function
def deci_to_any_list():
n1 = input("Enter a number: ")
n = int(n1)
radix1 = input("Enter a radix: ")
radix = int(radix1)
converted_number = []
if n>0:
if 2<=radix<=16:
while int(n/radix) == 0:
converted_number.append(n % radix)
converted_number.reverse()
x = ''.join(str(i) for i in converted_number)
x.replace('10', 'A')
x.replace('11', 'B')
x.replace('12', 'C')
x.replace('13', 'D')
x.replace('14', 'E')
x.replace('15', 'F')
x.replace('16', 'G')
else:
converted_number.append(n % radix)
else:
print("Wrong input!!")
else:
print("Wrong input!!")
print("%d in base 10 is %d in base %d" % (n1,x,radix1))
deci_to_any_list()
Enter a number : 10
Enter a radix : 2
我想创建一个小数转换函数。我想使用追加、反向、连接和替换函数创建此函数。如果我输入上面的代码,它会显示 name 'x' is not defined
。我该如何处理?
你的代码中有很多不一致之处。
您正在打印 x
而没有更新它。
python 字符串是不可变的,一旦你 apply
替换你必须用另一个变量捕获副本。
你的碱基转换逻辑也不对
更正后的代码:
def replace_base(x):
x = x.replace('10', 'A')
x = x.replace('11', 'B')
x = x.replace('12', 'C')
x = x.replace('13', 'D')
x = x.replace('14', 'E')
x = x.replace('15', 'F')
x = x.replace('16', 'G')
return x
def deci_to_any_list():
n1 = input("Enter a number: ")
n = int(n1)
radix1 = input("Enter a radix: ")
radix = int(radix1)
converted_number = []
x = None # initialize
if n>0:
if 2<=radix<=16:
while n>0:
# conversion part
converted_number.append(n % radix)
n = n//radix
# once the conversion is done
converted_number.reverse()
x = ''.join(replace_base(str(i)) for i in converted_number)
else:
print("Wrong input!!")
else:
print("Wrong input!!")
print(converted_number)
print(x)
deci_to_any_list()
def deci_to_any_list():
n1 = input("Enter a number: ")
n = int(n1)
radix1 = input("Enter a radix: ")
radix = int(radix1)
converted_number = []
if n>0:
if 2<=radix<=16:
while int(n/radix) == 0:
converted_number.append(n % radix)
converted_number.reverse()
x = ''.join(str(i) for i in converted_number)
x.replace('10', 'A')
x.replace('11', 'B')
x.replace('12', 'C')
x.replace('13', 'D')
x.replace('14', 'E')
x.replace('15', 'F')
x.replace('16', 'G')
else:
converted_number.append(n % radix)
else:
print("Wrong input!!")
else:
print("Wrong input!!")
print("%d in base 10 is %d in base %d" % (n1,x,radix1))
deci_to_any_list()
Enter a number : 10
Enter a radix : 2
我想创建一个小数转换函数。我想使用追加、反向、连接和替换函数创建此函数。如果我输入上面的代码,它会显示 name 'x' is not defined
。我该如何处理?
你的代码中有很多不一致之处。
您正在打印
x
而没有更新它。python 字符串是不可变的,一旦你
apply
替换你必须用另一个变量捕获副本。你的碱基转换逻辑也不对
更正后的代码:
def replace_base(x):
x = x.replace('10', 'A')
x = x.replace('11', 'B')
x = x.replace('12', 'C')
x = x.replace('13', 'D')
x = x.replace('14', 'E')
x = x.replace('15', 'F')
x = x.replace('16', 'G')
return x
def deci_to_any_list():
n1 = input("Enter a number: ")
n = int(n1)
radix1 = input("Enter a radix: ")
radix = int(radix1)
converted_number = []
x = None # initialize
if n>0:
if 2<=radix<=16:
while n>0:
# conversion part
converted_number.append(n % radix)
n = n//radix
# once the conversion is done
converted_number.reverse()
x = ''.join(replace_base(str(i)) for i in converted_number)
else:
print("Wrong input!!")
else:
print("Wrong input!!")
print(converted_number)
print(x)
deci_to_any_list()