这可能是我想念的愚蠢但是:
我得到了这个例外:
Traceback (most recent call last):
File "P:\Programming\Python\Windows\Console\ProjectHashDec\ProjectHashDec\ProjectHashDec.py", line 29, in <module>
print(str(j) + ": " + str(int(hex(convertNumbers[j]), times)))
TypeError: 'str' object cannot be interpreted as an integer
我不知道为什么这段代码不起作用:
print("How many hexadecimal numbers do you want to input?")
print("A: 8")
print("B: 16")
print("C: 32")
print("D: 64")
choice = input("What do you choose? > ")
times = 0
if (choice == "A"):
times = 8
elif (choice == "B"):
times = 16
elif (choice == "C"):
times = 32
elif (choice == "D"):
times = 64`
convertNumbers = []
i = 0
j = 0
while i < times:
i += 1
convertNumbers.append(input(str(i) + ": "))
while j < times:
j += 1
print(str(j) + ": " + str(int(hex(convertNumbers[j]), times)))
答案 0 :(得分:0)
为了让你的代码能够运行,你需要在时间= 64时删除反引号(会给你语法错误),删除“int”(int将不适用于十六进制数字),并更改对convertnumbers的索引调用(第一个索引是0而不是1)。
我不会更改代码中的任何其他内容,但您可以采取一些简单的方法来改进代码。例如,如果userinput不是a,b,c或d,则可以考虑添加一个逻辑,指定所有输入应该是字符串,减少while循环和所需的userinputs数量。
print("How many hexadecimal numbers do you want to input?")
print("A: 8")
print("B: 16")
print("C: 32")
print("D: 64")
choice = input("What do you choose? > ")
times = 0
if (choice == "A"):
times = 8
elif (choice == "B"):
times = 16
elif (choice == "C"):
times = 32
elif (choice == "D"):
times = 64
convertNumbers = []
i = 0
j = 0
while i < times:
i += 1
convertNumbers.append(input(str(i) + ": "))
while j < times:
j += 1
print(str(j) + ": " + str((hex(convertNumbers[j-1]), times)))