在Python3中可以做一个前面加0的十进制变量吗?
Is it possible to make a decimal variable with a 0 in front of it in Python 3?
我正在开展一个项目,该项目要求变量 s
等于 01234567
。但是,每当我尝试 运行 程序时,它总是说
leading zeros in decimal literals are not permitted; use an 0o prefix for octal integers
。 s 必须以零开头;有什么办法可以绕过这个错误吗?
k = list(int(input("What is the key? "))) # asks user for key
s = 01234567 # state to shuffle the numbers
# this line above is where the error takes place
#still a work in progress
def hash(s, k):
i = 0
j = 0
for i < 8:
j = (j + s(i) + k(i % 5)) % 8 # % performs mod
# j makes a new j variable
s(i), s(j) = s(j), s(i) # swaps the two variable values
hash (s, k) # hashes
首先,您应该让 k
和 s
成为一个整数列表。字符串是不可变的,因此您将无法就地编辑它们。此外,您需要使用方括号 ([]
) 进行索引。您不能使用括号 (()
) 进行索引。总计:
k = [int(c) for c in input("What is the key? ")] # asks user for key
s = [0,1,2,3,4,5,6,7] # state to shuffle the numbers
# this line above is where the error takes place
#still a work in progress
def hash(s, k):
i = 0
j = 0
for i in range(8):
j = (j + s[i] + k[i % 5]) % 8 # % performs mod
# j makes a new j variable
s[i], s[j] = s[j], s[i] # swaps the two variable values
hash (s, k) # hashes
print (s)
最后,您可能需要检查 k 的长度是否至少为 5 以防止索引错误。
我正在开展一个项目,该项目要求变量 s
等于 01234567
。但是,每当我尝试 运行 程序时,它总是说
leading zeros in decimal literals are not permitted; use an 0o prefix for octal integers
。 s 必须以零开头;有什么办法可以绕过这个错误吗?
k = list(int(input("What is the key? "))) # asks user for key
s = 01234567 # state to shuffle the numbers
# this line above is where the error takes place
#still a work in progress
def hash(s, k):
i = 0
j = 0
for i < 8:
j = (j + s(i) + k(i % 5)) % 8 # % performs mod
# j makes a new j variable
s(i), s(j) = s(j), s(i) # swaps the two variable values
hash (s, k) # hashes
首先,您应该让 k
和 s
成为一个整数列表。字符串是不可变的,因此您将无法就地编辑它们。此外,您需要使用方括号 ([]
) 进行索引。您不能使用括号 (()
) 进行索引。总计:
k = [int(c) for c in input("What is the key? ")] # asks user for key
s = [0,1,2,3,4,5,6,7] # state to shuffle the numbers
# this line above is where the error takes place
#still a work in progress
def hash(s, k):
i = 0
j = 0
for i in range(8):
j = (j + s[i] + k[i % 5]) % 8 # % performs mod
# j makes a new j variable
s[i], s[j] = s[j], s[i] # swaps the two variable values
hash (s, k) # hashes
print (s)
最后,您可能需要检查 k 的长度是否至少为 5 以防止索引错误。