我很难过,我不知道为什么这不起作用。除了else语句之外,它只会忽略所有内容。不知道是什么导致了这个,帮助!
这个程序太简单了,但是,它在这里,不起作用。
def main(): #the main function, where the program is held.
print("Opening Account List now...\n")#flavor tx to add user context for the list of numbers to be displayed.
infile = open('CHarge Accounts.txt', 'r')#opens the 'CHarge Accounts' text file in read-only mode,
#and saves it to the variable 'infile'.
numbers = infile.readlines()#The '.readlines()' command opens the text file stored in the 'infile'
#variable and and reads every single line of text in the file.
#It then saves the data that has been read to the variable 'numbers'.
infile.close()#This closes the text file being within the 'infile' variable, preventing data from being los,
#and causing errors in the program
index = 0#control value for the number of values to be inserted into the 'numbers' list.
#
while index < len(numbers): #while the index value is less than the number of values within numbers.
#This means that as long as index is less than however many account numbers
#that are within the 'numbers' list, the loop will continue.
numbers[index] = int(numbers[index])#While the loop runs, the values in the numbers index will be
#converted to integer values
index += 1 #controlling value increments for every number read
print (numbers, '\n')
x = 0 #control value for the while loop.
while x == 0: #Loop begins.
accnumber = int(input("Please type in the account number you'd like to change, or type in -1 to exit."))
if accnumber not in numbers and not -1:#Checks if account number is not in the saved txt file data, and
#if the user has inputted the 'kill' value.
print("The account number you have typed in is invalid.")#informs user data is invalid
elif accnumber in numbers and not -1:#Checks if the account number is within the saved text file data, and
#if the user has inputted the 'kill' value.
print("The account number you have selected is valid.")#text informs user that data is valid
elif accnumber == -1:#Checks if the account number value is -1
print("Goodbye!")#Flavor for the user
x = 1 #activates the control variable to break the loop
main()
答案 0 :(得分:0)
if accnumber not in numbers and not -1:
# ^^^^^^
当评估为真值语句时,0为False
,任何其他整数为True
。因此-1
为True
,not -1
为False
,(anything) and not -1
始终为False
。
因此,您的if
和elif
条款总是被跳过。
尝试
if accnumber not in numbers and accnumber != -1:
代替。
此外:
您的评论过度到了模糊您的计划的程度。我认为这是某种类要求,即评论每一行?
您使用while
循环和索引变量是非惯用的,可以用列表推导替换如下:
def main():
print("Opening Account List now...\n")
with open('Charge Accounts.txt') as inf:
numbers = [int(line) for line in inf]
print(numbers, '\n')
while True:
accnumber = int(input("Please enter an account number (or -1 to exit): "))
if accnumber == -1:
print("Goodbye!")
break
elif accnumber not in numbers:
print("The account number you have typed in is invalid.")
else:
print("The account number you have selected is valid.")
main()
答案 1 :(得分:0)
你的代码有点乱。当您添加评论时,请尽量使其易于阅读。另外,花更多时间在基本语法上!
def main():
print("Opening Account List now...\n")
infile = open('CHarge Accounts.txt', 'r')
numbers = infile.readlines()
infile.close()
index = 0
while index < len(numbers):
numbers[index] = int(numbers[index])
index += 1
print (numbers, '\n')
x = 0 #control value for the while loop.
while x == 0:
accnumber = int(input("Please type in the account number you'd like to change, or type in -1 to exit."))
if (accnumber not in numbers) and (accnumber !=-1):
print("The account number you have typed in is invalid.")
elif (accnumber in numbers) and (accnumber !=-1):
print("The account number you have selected is valid.")#text informs user that data is valid
elif accnumber == -1:#Checks if the account number value is -1
print("Goodbye!")#Flavor for the user
x = 1 #activates the control variable to break the loop
main()