程序忽略if和elif语句
我现在很困惑,完全不知道为什么这个代码不管用。它只执行了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()
2 个回答
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()
0
if accnumber not in numbers and not -1:
# ^^^^^^
在判断真假时,数字0被认为是False
,而其他任何整数都被认为是True
。所以-1
就是True
,而not -1
就是False
,并且(任何东西) 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()