当我尝试乘以一个数字时,它只是按次数复制它。python

2024-09-30 22:23:56 发布

您现在位置:Python中文网/ 问答频道 /正文

#Initialising Variables
inputISBN = input("What is your 10 digit ISBN number? ")
itemInList = 0
timesNumber = 11
listISBN = []
timesISBN = []
#Checking if the input is only numbers
if len(inputISBN) == 10 and inputISBN.isdigit() :
    while itemInList < 10 :
      listISBN.append(inputISBN[itemInList])
      itemInList = itemInList + 1
    print(listISBN)
    itemInList = 0
    while itemInList < 10 :
        num = listISBN[itemInList]
        int(num)
        timesISBN.append(num * timesNumber)
        itemInList = itemInList + 1
        timesNumber = timesNumber - 1
    print(timesISBN)
else:
    print("Invalid input")

HALP它只需打印输入数字11次,然后再打印10次 对不起,没有什么要说的了,我必须补充更多的细节。 此代码旨在将输入乘以11,然后乘以10,以此类推,但它只将数字复制了那么多。我不明白为什么这不起作用


Tags: inputifis数字variablesnumprintappend
2条回答

以下是一种更简单的方法:

>>> inputISBN=input("What is your 10 digit ISBN number? ")
What is your 10 digit ISBN number? 1203874657
>>> if(len(inputISBN)==10 and inputISBN.isdigit()):
...     timesISBN = [int(i)*j for i,j in zip(inputISBN,range(11,1,-1))]
...     print(timesISBN)
... else:
...     print("Invalid input")
... 
[11, 20, 0, 24, 56, 42, 20, 24, 15, 14]

您需要存储int()调用的返回值;除此之外,num值不受影响:

num = listISBN[itemInList]
num = int(num)

相关问题 更多 >