我用python编写了多个函数的代码。我正在对我的代码进行单元测试,但主函数正在重复自己



我创建了一个在线销售系统的程序。它有一个主菜单和一些选项。我已经将每个组件实现为一个函数。但当我运行时,它运行得很好,并向我显示主菜单,但当我选择任何选项时,它会再次显示主菜单。我不确定我做得是否正确,我只是个初学者。这是代码;

def main_menu(): 
print("=======================================================")
print("ttWelcome to our online System")
print("=======================================================")
print("<1> List the available services")
print("<2> Search for a service")
print("<3> Purchase a service")
print("<4> List my services")
print("<5> Quit ")
selected_option = int(input("Please select an option from the above:"))
return selected_option
all_services = { '1': ['MS Word', '$20'], '2':['Photoshop', '$120'],
'3':['Music player','$89'], '4': ['Photo Advance', '$99'],
}
def option1(argu):
print('nThe available services and applications are:n')                
for key, value in all_services.items():
print(key, '-', value[0])
print("nThank You!")
def option2():
service_search = input("nPlease enter the service name you want to search or a negative number to exit:")
search_result = {}
for id, name in all_services.items():
if service_search in name[0].replace(" ","").lower():
search_result[id] = name 
if len(search_result) > 0:
print('n',len(search_result), 'services have been found:n')
for key, val in search_result.items():
print('Service ID:', key, 'nService Name:', val[0], 'nCost:', val[1])
else:
print('This service is not available')
another_search = input('Do you want to search for another course (Y/N)?')
if another_search == 'Y':
option2()
elif another_search == 'N':
main_menu()
else:
print('Exit')
def option3():
print("The available services are:n")
for id, name in all_services.items():
print(id,'-' ,name[0])
select_option = input('nPlease select the service code you want to purchase or a negative number to exit:')
print('nservice detail:nName:',all_services[select_option][0], 'nCost:', all_services[select_option][1])
confirm_purchase = input('nDo you want to buy this service (Y/N)?')
if confirm_purchase == 'Y':
print('Please enter your name and your credit card details:n')
customer_name = input('Name: ')
card_no = input('Card Number: ')
card_expmo = input('MM: ')
card_expy = input('YYYY:')
with open('customer.txt', 'w') as f:
f.write(customer_name + select_option + all_services[select_option][0]+ all_services[select_option][1])
print('Thank You!nYou have been purchasing the ',all_services[select_option][0],'.')
another_purchase = input('Do you want to purchase another service (Y/N)?')
if another_purchase == 'Y':
option3()
else:
print('Thankyou for purchasing from our online shop')
elif confirm_purchase == 'N':
print('n')
main_menu()
else:
print('Exit')
def option4():
name_search = input('Please enter your name to search:')
with open('/content/customer.txt', 'r') as info:
purchase_details = []
for line in info:
line_list = line.split(',')
purchase_details.append(line_list)
for i in range(len(purchase_details)):
if name_search in purchase_details[i][0]:
print('Hello', name_search,'you have purchased the following:n')
print(i,'-',purchase_details[i][2])

if __name__ == '__main__':
main_menu()
if main_menu() == 1:
option1(all_services)
elif main_menu() == 2:
option2()
elif main_menu() == 3:
option3()
elif main_menu() == 4:
option4()
elif main_menu() == 5:
print('Goodbye')
else:
print('Please enter a valid option')

以下是我从上面的代码中得到的输出;

=======================================================
Welcome to our online System
=======================================================
<1> List the available services
<2> Search for a service
<3> Purchase a service
<4> List my services
<5> Quit 
Please select an option from the above:5
=======================================================
Welcome to our online System
=======================================================
<1> List the available services
<2> Search for a service
<3> Purchase a service
<4> List my services
<5> Quit 
Please select an option from the above:5
=======================================================
Welcome to our online System
=======================================================
<1> List the available services
<2> Search for a service
<3> Purchase a service
<4> List my services
<5> Quit 
Please select an option from the above:5
=======================================================
Welcome to our online System
=======================================================
<1> List the available services
<2> Search for a service
<3> Purchase a service
<4> List my services
<5> Quit 
Please select an option from the above:5
=======================================================
Welcome to our online System
=======================================================
<1> List the available services
<2> Search for a service
<3> Purchase a service
<4> List my services
<5> Quit 
Please select an option from the above:5
=======================================================
Welcome to our online System
=======================================================
<1> List the available services
<2> Search for a service
<3> Purchase a service
<4> List my services
<5> Quit 
Please select an option from the above:5
Goodbye

每次编写main_menu()时,都会调用main_menu函数并导致菜单"重复本身"。

尝试:

if __name__ == '__main__':
choice = main_menu()
if choice == 1:
option1(all_services)
elif choice == 2:
option2()
elif choice == 3:
option3()
elif choice == 4:
option4()
elif choice == 5:
print('Goodbye')
else:
print('Please enter a valid option')

注意,我们只写main_menu()一次,所以它只被调用一次。这将至少解决重复出现的问题。

您可能还想考虑添加一个while循环,以允许用户在完成以下选项后继续使用菜单:

if __name__ == '__main__':
while True:  
choice = main_menu()
if choice == 1:
option1(all_services)
elif choice == 2:
option2()
elif choice == 3:
option3()
elif choice == 4:
option4()
elif choice == 5:
print('Goodbye')
break
else:
print('Please enter a valid option')

最新更新