In: Computer Science
Note: Variable names and function names are used in such a way for a better understanding. These can be changed according to the user's choice. Please refer to code snippets for a better understanding of comments and indentations.
IDE Used: PyCharm
Program Code
# function to get number of serving
def numServing(len_inch):
num_serve = len_inch//3
return num_serve
# function to get cost per serving
def costPerServing(sand_cost, num_serve):
cost_per_serve = sand_cost/num_serve
return cost_per_serve
# main function
def main():
# variable to ask if order more sandwich
more_sub = 'y'
# loop until user input 'n'
while more_sub == 'y':
print("Give Sandwich Order Details\n")
# promt user for sandwich length and cost
len_inch = float(input("Sandwich length in inches : "))
sand_cost = float(input("Sandwich cost : $"))
# call function to get number of serve and cost per serve
num_serve = int(numServing(len_inch))
cost_per_serve = costPerServing(sand_cost, num_serve)
# display the final values
# i.e number of serve and cost of each serve
print("Number of Serving : ", num_serve)
print("Cost per serving : $%0.2f" %cost_per_serve)
# prompt to know if want more sub
more_sub = input("Request another sub ? (y/n) : ")
if more_sub == 'n':
print("Thank You for your Sandwich order!")
print("\n")
# main declaration
if __name__=="__main__":
main()
Code Snippets


Sample Input Output
