如何在 if 循环中中断代码?

how do you break code in a if loop?

好的两个问题,我的导师希望我在 else 语句之后破坏我的代码,因为 menu() 函数在 "customer not found" 之后不断重复,但我不明白她的意思也出于某种原因我的代码仅使用 "customers" 列表中的第一个名字而不是所有名字运行,如果有人能指出我代码中的缺陷,那将非常感谢。

#Program 3 BankApp

def customerind(customer):
    ind = ""
    for i in range(len(customers)):
        if customer == customers[i]:
            ind = i
        if ind != "":
            return ind
        else:
            print("customer not found")

def printbalance(index):
    print("your remaining balance is", balances[index])
def menu():
    print("type D to deposit money", customer)
    print("type W to withdraw money", customer)
    print("type B to display balance", customer)
    print("type C to change user", customer)
    print("type E to exit", customer)
def withdraw(index, withdrawAmt):
    if withdrawAmt < balances[index]:
        balances[index] = balances[index] - withdrawAmt
    else:
        print("you went over your balance")
def deposit(index, depositAmt):
        balances[index] = balances[index] + depositAmt















global customers
customers= ["Mike", "Jane", "Steve"]
global balances
balances= [300, 300, 300]
global index
customer= input("what is your name?")
index= customerind(customer)
printbalance(index)


answer= ""
while answer != "E":
    menu()
    answer= input("what is your menu choice?")
    if answer=="C":
        customer= input("who are you?")
        index= customerind(customer)
    if answer== "W":
        withdrawAmt = float(input("how much did you want to withdraw today?"))
        withdraw(index, withdrawAmt)
        printbalance(index)
    if answer=="B":
        printbalance(index)
    if answer=="D":
        depositAmt = float(input("how much did you want to deposit today?"))
        deposit(index, depositAmt)
        printbalance(index)

你这里有一些问题。

1. "for some reason my code only runs using only the first name in the "客户列表

在您的 customerind 函数中,您仅在第一次迭代后打印 "customer not found"。您需要将 else 语句移到 for 循环之外,以便它可以遍历所有名称。

def customerind(customer):
  #Iterate through each name, and see if there is a match.
  for i in range(len(customers)):
    if customer == customers[i]:
      return i #If we found the name, instantly return the index. 
  #Outside of the for loop, will get called once above finishes iterating through all names.
  return -1 #Return -1 if no match, since the index can be any value 0 -> Infinity

所以这个函数现在的工作方式是,如果在 for 循环中找到名称,那么它 return 就是索引。如果遍历所有名称后仍未找到该名称,则该函数将 return -1

既然这个函数设置好了一点,让我们看看底部的主要代码...

只要用户不键入 E

menu() 就会不断被调用。那么如果你想在没有找到客户的情况下自动退出,你应该这样做:

answer= ""
while answer != "E":
    menu()
    answer= input("what is your menu choice?")
    if answer=="C":
        customer= input("who are you?")
        index= customerind(customer)
        if index == -1:
          print('Customer not found. Exiting...')
          break #Break out of the while loop above so the program exits. 
    if answer== "W":
        withdrawAmt = float(input("how much did you want to withdraw today?"))
        withdraw(index, withdrawAmt)
        printbalance(index)
    if answer=="B":
        printbalance(index)
    if answer=="D":
        depositAmt = float(input("how much did you want to deposit today?"))
        deposit(index, depositAmt)
        printbalance(index)