【发布时间】:2018-05-13 01:36:35
【问题描述】:
以下是我的代码,我遇到问题的部分位于第 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”更改为 Integer,因为我遇到了一个令人沮丧的错误:
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
是否有可能收到有关如何改进我的代码的任何额外信息,以什么方式以及如何将“cardNo”列表从“NoneType”转换为可下标的东西,因为我已经尝试了所有可能的方法。另外,如果有任何反馈空间来提高代码效率,那将是非常有利的。
提前致谢!
【问题讨论】:
-
您的问题是
cardNo.reverse()返回None。该列表已重新排序,因此您不应将结果分配回cardNo -
非常类似于this question中的
list.sort()。
标签: python-3.x python-3.5