在Python上,如何只显示列表中的数字而不显示逗号和括号?
最后,帐号显示为在Python上,如何只显示列表中的数字而不显示逗号和括号?,python,algorithm,Python,Algorithm,最后,帐号显示为[1,2,3,4,5,6,7]。如何将其显示为1234567,而不使用逗号和方括号 #This will ask the user to enter a 7 digit number #then it would calculate the modulus 11 check digit #then would show the user the complete 8 digit number weight=[8,7,6,5,4,3,2] number= input("Pleas
[1,2,3,4,5,6,7]
。如何将其显示为1234567
,而不使用逗号和方括号
#This will ask the user to enter a 7 digit number
#then it would calculate the modulus 11 check digit
#then would show the user the complete 8 digit number
weight=[8,7,6,5,4,3,2]
number= input("Please enter your 7 digit number: ")
#If the user enters more than 7 characters it will prompt the user to try again
while len(number) > 7:
number=input ("Error! Only 7 numbers allowed! Try again: ")
#This puts the 7 digit number into a list
account_number=number
#This converts the string in the list into a integer
account_number = [int(i) for i in account_number]
#This separates the numbers into multple values every character
list(account_number)
#This multiplies the account number by the weight
num1=weight[0]*account_number[0]
num2=weight[1]*account_number[1]
num3=weight[2]*account_number[2]
num4=weight[3]*account_number[3]
num5=weight[4]*account_number[4]
num6=weight[5]*account_number[5]
num7=weight[6]*account_number[6]
#This adds up the all the answers above
num8=num7+num6+num5+num4+num3+num2+num1
#The use of % divides the number and gives the remainder
remainder=num8%11
#This generates the final check digit
check_number=11-remainder
#This adds the check number to the 7 digit number (account number)
account_number.insert(7,"{0}".format(check_number))
#This gives the user the total 8 digit number
print ("Your account number is {0}".format(account_number))
这首先将int转换为str,然后不使用空格将它们连接在一起
例如:
>>> "".join(map(str,account_number))
在您的情况下,更改为:
>>> a = [1,2,3]
>>> map(str,a)
['1', '2', '3']
>>> "".join(map(str,a))
'123'
>>>
评论的后续行动:
account_number = "".join(map(str, account_number))
print ("Your account number is {0}".format(account_number))
你能告诉我你的代码在我的代码中的位置吗?因为当我试着把它放进去的时候,我得到的结果很复杂。谢谢,非常感谢much@TheIIIJDC如果答案对你有帮助,别忘了接受它@joel goldstick你知道我将如何使用户输入精确到7而不是更少(如果他们输入的数字超过7,我已经做了位,如果他们输入的数字少于7,我只需要做位),这确实是一个不同的问题,但不是测试>7测试==7
while len(number) != 7:
number=input ("Error! Only 7 numbers allowed! Try again: ")