你如何打破if循环中的代码?

时间:2016-05-26 23:50:02

标签: python-3.3

好的两个问题我的导师希望我在我的else语句之后破坏我的代码因为menu()函数在“找不到客户”之后不断重复但是我不明白她的意思也因为某些原因我的代码只运行只使用“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 个答案:

答案 0 :(得分:0)

这里有一些问题。

<强> 1。 &#34;由于某种原因,我的代码只使用&#34;客户&#34;中的第一个名称运行列表&#34;

customerind功能中,您只在第一次迭代后打印&#34;未找到客户&#34; 。您需要将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循环中找到该名称,则返回索引。如果在遍历所有名称后未找到该名称,则该函数将返回-1

现在这个功能设置得更好了,让我们看一下底部的主要代码...

只要用户未键入menu(),就会不断调用{p> E。那么如果你想在找不到客户时自动退出,你应该这样做:

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)