如何从' NoneType'更改列表元素类型?只是整数?

时间:2017-11-29 10:59:08

标签: python-3.x python-3.5

以下是我的代码,我遇到问题的部分是第19行。

#Check It Out Python 3.5.2 program
import datetime
import CheckItOutprogram
counter = 1

currentYear = datetime.date.today().year
currentMonth = datetime.date.today().month
dayOfMonth = datetime.date.today().day

name = input("Please input your name: ")
postCode = input("Please enter your postcode: ")
expiryYear = int(input("Please enter the year your card expires: "))
expiryMonth = int(input("Please enter the month your card expires: "))
expiryDay = int(input("Please enter the day of the month your card expires: "))
cardNo = input("Please enter your card number: ")
checkDigit = int(cardNo[7]) / 10
cardNo = list(cardNo)
cardNo = cardNo.reverse()
cardNoOdd = int(cardNo[0]) + int(cardNo[2]) + int(cardNo[4]) + int(cardNo[6])

#The three following IF statements check the expiry date.
if expiryYear > currentYear:
    print("I'm sorry, your card has expired.")
    again = input("Would you like to start again? Press enter to exit or type anything to start again: ")
    if again != "":
        exit()
    else:
        CheckItOutprogram.start()
if (expiryMonth >= currentMonth) and (expiryDay > dayOfMonth):
    print("I'm sorry, your card has expired.")
    again = input("Would you like to start again? Press enter to exit or type anything to start again: ")
    if again != "":
        exit()
    else:
        CheckItOutprogram.start()
if (expiryYear != isinstance(expiryYear, int)) or (expiryMonth != isinstance(expiryMonth, int)) or (expiryDay != isinstance(expiryDay, int)):
    print("I'm sorry, your card has expired.")
    again = input("Would you like to start again? Press enter to exit or type anything to start again: ")
    if again != "":
        exit()
    else:
        CheckItOutprogram.start()
#This IF statement checks the amount of characters the card number contains and makes sure that it has 8 elements.
if (len(cardNo) > 8) or (len(cardNo) * 8):
    print("Please try again...\n")
    CheckItOutprogram.start()

#This checks if the card is avaliable for a 10% discount using the last number.
if checkDigit == isinstance(checkDigit, int):
    print("This card is valid! This means that you may have a 10% discount off everything!")
    print("Here are the customer and loyalty card details. Name: " + name + ". Post Code:  " + postCode + ". Expiry Date: " + str(expiryDay) + "/" + str(expiryMonth) + "/" + str(expiryYear) + ". Card Number: " + cardNo)
else:
    print("I'm sorry, your card isn't qualified for a 10% discount.")
    again = input("Would you like to start again? Press enter to exit or type anything to start again: ")
    if again != "":
        exit()

有没有办法从' NoneType'更改列表类型?为了一个整数,因为我得到一个令人沮丧的错误说:

Traceback (most recent call last):
  File "Z:\Computer Science\Check It Out program\CheckItOutprogram.py", line 3, in <module>
    import CheckItOutprogram
  File "Z:\Computer Science\Check It Out program\CheckItOutprogram.py", line 20, in <module>
    cardNoOdd = int(cardNo[0]) + int(cardNo[2]) + int(cardNo[4]) + int(cardNo[6])
TypeError: 'NoneType' object is not subscriptable

是否有可能收到有关如何改进我的代码的任何额外信息,以及如何转换&#39; cardNo&#39;来自&#39; NoneType&#39;的列表因为我已经尝试了所有可能的东西,这是可订阅的东西。此外,如果有任何反馈空间来提高代码效率,那将非常有利。

提前致谢!

1 个答案:

答案 0 :(得分:0)

我得到了一个结果,添加了以下内容:

cardNo = str(input("Please enter your card number: "))
cardNo = list(cardNo)
cardNo = cardNo[::-1]
print(cardNo)
cardNoOdd = int(cardNo[0]) + int(cardNo[2]) + int(cardNo[4]) + int(cardNo[6])
print(cardNoOdd)


my input: Please enter your card number: 1548315414
out: ['4', '1', '4', '5', '1', '3', '8', '4', '5', '1']
out: 17
编辑(对不起,忘了解释):我没有使用反向本身,但我使用索引来反转列表。另外,我将输入定义为字符串。

希望这就是你要找的东西!