def代码似乎以某种方式影响了我的代码

时间:2016-06-30 21:53:31

标签: function python-3.x

在我添加'def menu()'代码和'menu()'代码之前,我的代码运行正常,但现在它似乎无法正常工作!任何想法为什么不呢?我只是得不到回报,只是'>>>'!仅供参考:所有缩进都是正确的,因为它们在类似代码上没问题,并且在将“def”函数引入代码之前工作正常。

def menu() :
     print("Please enter the number corresponding with the option you would like to pick.")
     print("1. Record Purchase(s)")
     print("2. Retrieve Previous Purchase(s)")
     option=input("> ")

 if option == "1":
      customer_id1=input("Customer ID: ")
      file=open(customer_id1.capitalize()+".txt","a")
      info=input("Please enter all purchases seperated by commas: ")
      file.write(info+"\n")
      file.close()
      print("PURCHASE RECORD SUCCESSFUL")
      import time
      time.sleep(5)
      menu()

 if option == "2":
      customer_id2=input("Customer ID: ")
      file=open(customer_id2+".txt","r")
      print(file.read())
      time.sleep(10)
      menu()

 else:
      print("Sorry, that doesnt seem to be an option, returning to menu.")
      menu()

1 个答案:

答案 0 :(得分:0)

提及>>>我假设你在python shell中运行它。在这种情况下,您的问题是您从未实际调用menu()。你只需定义它。如果你想调用它,你必须在函数声明的范围之外放置一个menu()。