我正在尝试制作一个程序,将任何基数中的数字转换为用户选择的另一个基数.到目前为止我的代码是这样的:
innitvar = float(raw_input("Please enter a number: "))
basevar = int(raw_input("Please enter the base that your number is in: "))
convertvar = int(raw_input("Please enter the base that you would like to convert to: "))
Run Code Online (Sandbox Code Playgroud)
这些是我从用户那里获得的数据.初始数字,初始基数和用户想要转换的基数.据我了解,我需要转换为基数10,然后转换为用户指定的所需基数.
这就是我撞墙的地方:我需要将初始数字中最左边的数字乘以其初始基数,然后将下一个数字加到右边,然后重复直到我点到最右边的数字.我理解如何在纸上做到这一点,但我不知道如何将它放入Python代码中.我不确定如何将第一个数字相乘,然后添加下一个数字,也不知道如何让程序知道何时停止执行此操作.
我不是要求为我编写程序,但我想指出正确的方向.
谢谢你的时间!
这应该是问题答案的前半部分.你能弄清楚如何转换成基地吗?
# Create a symbol-to-value table.
SY2VA = {'0': 0,
'1': 1,
'2': 2,
'3': 3,
'4': 4,
'5': 5,
'6': 6,
'7': 7,
'8': 8,
'9': 9,
'A': 10,
'B': 11,
'C': 12,
'D': 13,
'E': 14,
'F': 15,
'G': 16,
'H': 17,
'I': 18,
'J': 19,
'K': 20,
'L': 21,
'M': 22,
'N': 23,
'O': 24,
'P': 25,
'Q': 26,
'R': 27,
'S': 28,
'T': 29,
'U': 30,
'V': 31,
'W': 32,
'X': 33,
'Y': 34,
'Z': 35,
'a': 36,
'b': 37,
'c': 38,
'd': 39,
'e': 40,
'f': 41,
'g': 42,
'h': 43,
'i': 44,
'j': 45,
'k': 46,
'l': 47,
'm': 48,
'n': 49,
'o': 50,
'p': 51,
'q': 52,
'r': 53,
's': 54,
't': 55,
'u': 56,
'v': 57,
'w': 58,
'x': 59,
'y': 60,
'z': 61,
'!': 62,
'"': 63,
'#': 64,
'$': 65,
'%': 66,
'&': 67,
"'": 68,
'(': 69,
')': 70,
'*': 71,
'+': 72,
',': 73,
'-': 74,
'.': 75,
'/': 76,
':': 77,
';': 78,
'<': 79,
'=': 80,
'>': 81,
'?': 82,
'@': 83,
'[': 84,
'\\': 85,
']': 86,
'^': 87,
'_': 88,
'`': 89,
'{': 90,
'|': 91,
'}': 92,
'~': 93}
# Take a string and base to convert to.
# Allocate space to store your number.
# For each character in your string:
# Ensure character is in your table.
# Find the value of your character.
# Ensure value is within your base.
# Self-multiply your number with the base.
# Self-add your number with the digit's value.
# Return the number.
def str2int(string, base):
integer = 0
for character in string:
assert character in SY2VA, 'Found unknown character!'
value = SY2VA[character]
assert value < base, 'Found digit outside base!'
integer *= base
integer += value
return integer
Run Code Online (Sandbox Code Playgroud)
这是解决方案的后半部分.通过使用这两个函数,转换碱基非常容易.
# Create a value-to-symbol table.
VA2SY = dict(map(reversed, SY2VA.items()))
# Take a integer and base to convert to.
# Create an array to store the digits in.
# While the integer is not zero:
# Divide the integer by the base to:
# (1) Find the "last" digit in your number (value).
# (2) Store remaining number not "chopped" (integer).
# Save the digit in your storage array.
# Return your joined digits after putting them in the right order.
def int2str(integer, base):
array = []
while integer:
integer, value = divmod(integer, base)
array.append(VA2SY[value])
return ''.join(reversed(array))
Run Code Online (Sandbox Code Playgroud)
将所有内容放在一起后,您应该最终得到以下程序.请花点时间搞清楚!
innitvar = raw_input("Please enter a number: ")
basevar = int(raw_input("Please enter the base that your number is in: "))
convertvar = int(raw_input("Please enter the base that you would like to convert to: "))
# Create a symbol-to-value table.
SY2VA = {'0': 0,
'1': 1,
'2': 2,
'3': 3,
'4': 4,
'5': 5,
'6': 6,
'7': 7,
'8': 8,
'9': 9,
'A': 10,
'B': 11,
'C': 12,
'D': 13,
'E': 14,
'F': 15,
'G': 16,
'H': 17,
'I': 18,
'J': 19,
'K': 20,
'L': 21,
'M': 22,
'N': 23,
'O': 24,
'P': 25,
'Q': 26,
'R': 27,
'S': 28,
'T': 29,
'U': 30,
'V': 31,
'W': 32,
'X': 33,
'Y': 34,
'Z': 35,
'a': 36,
'b': 37,
'c': 38,
'd': 39,
'e': 40,
'f': 41,
'g': 42,
'h': 43,
'i': 44,
'j': 45,
'k': 46,
'l': 47,
'm': 48,
'n': 49,
'o': 50,
'p': 51,
'q': 52,
'r': 53,
's': 54,
't': 55,
'u': 56,
'v': 57,
'w': 58,
'x': 59,
'y': 60,
'z': 61,
'!': 62,
'"': 63,
'#': 64,
'$': 65,
'%': 66,
'&': 67,
"'": 68,
'(': 69,
')': 70,
'*': 71,
'+': 72,
',': 73,
'-': 74,
'.': 75,
'/': 76,
':': 77,
';': 78,
'<': 79,
'=': 80,
'>': 81,
'?': 82,
'@': 83,
'[': 84,
'\\': 85,
']': 86,
'^': 87,
'_': 88,
'`': 89,
'{': 90,
'|': 91,
'}': 92,
'~': 93}
# Take a string and base to convert to.
# Allocate space to store your number.
# For each character in your string:
# Ensure character is in your table.
# Find the value of your character.
# Ensure value is within your base.
# Self-multiply your number with the base.
# Self-add your number with the digit's value.
# Return the number.
integer = 0
for character in innitvar:
assert character in SY2VA, 'Found unknown character!'
value = SY2VA[character]
assert value < basevar, 'Found digit outside base!'
integer *= basevar
integer += value
# Create a value-to-symbol table.
VA2SY = dict(map(reversed, SY2VA.items()))
# Take a integer and base to convert to.
# Create an array to store the digits in.
# While the integer is not zero:
# Divide the integer by the base to:
# (1) Find the "last" digit in your number (value).
# (2) Store remaining number not "chopped" (integer).
# Save the digit in your storage array.
# Return your joined digits after putting them in the right order.
array = []
while integer:
integer, value = divmod(integer, convertvar)
array.append(VA2SY[value])
answer = ''.join(reversed(array))
# Display the results of the calculations.
print answer
Run Code Online (Sandbox Code Playgroud)
我需要将初始数字中最左边的数字乘以其原始基数,然后将下一个数字加到右边,然后重复直到我点到最右边的数字.
所以你需要得到数字.在列表中.
提示1:使用divmod()函数将数字分成数字.除以10得到十进制数字.
提示2:当n > 0时:你可以divmod()用来得到商和余数.如果将剩余部分保存在列表中,并使用商作为n的新值,则数字会变小,直到剩下的数字为零并且您已完成.
提示3:您的数字按从右到左的顺序排列.使用reverse切换的这个困扰你的列表的顺序.或者使用创建列表insert(0,digit).
现在你有了数字.在列表中.您可以遍历列表.
尝试for声明大小.
您可能需要使用"多个并添加"循环. total = total * new_base + next_digit是循环体看起来的方式.