crzcas

reusing code with function

Dec 9th, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.03 KB | None | 0 0
  1. import math
  2.  
  3. def do_calculation():   # function that replace some code in add and subtract operations
  4.     print("lets " + command + " some numbers")
  5.     input1 = input("Number 1> ")
  6.     input2 = input("Number 2> ")
  7.     number1 = int(input1)
  8.     number2 = int(input2)
  9.     if command == "add":
  10.         result = number1 + number2
  11.         operator = " + "
  12.     elif command == "subtract":
  13.         result = number1 - number2
  14.         operator = " - "
  15.     output = str(result)
  16.     print(input1 + operator + input2 + " = " + output)
  17.  
  18.  
  19. finished = False
  20. while finished == False:
  21.  
  22.     print()
  23.     print("Hi, I am Atom, your personal bot.")
  24.     print("You can select one word of the menu, writing the word:")
  25.     print("add   subtract   multiply   divide   volume   average   shopping   bye")
  26.     command = input("How can I help you? ")
  27.  
  28.     # This if works when the word of the menu is wrong, giving a message "Sorry, I don't understand."
  29.     if command !="add" and command !="subtract" and command !="multiply" and command !="divide" and command !="volume" and command !="average" and command !="shopping" and command != "bye":
  30.         print("Sorry, I don't understand.")
  31.  
  32.     elif command == "add" or command == "plus":   # Adding code with function do_calculation() in line 30
  33.         do_calculation()
  34.  
  35.     elif command == "subtract":   # Adding code with function do_calculation() in line 36
  36.         do_calculation()
  37.        
  38.     elif command == "multiply" or command == "multiplication":   # Adding code to multiply in lines 38-46
  39.         print("lets multiply two numbers")
  40.         input1 = input("Number 1> ")
  41.         input2 = input("Number 2> ")
  42.         number1 = float(input1)
  43.         number2 = float(input2)
  44.         result = number1 * number2
  45.         output = str(result)
  46.         print(input1 + " * " + input2 + " = " + output)
  47.  
  48.     elif command == "divide" or command == "division":   # Adding code to divide in lines 48-56
  49.         print("lets divide two numbers")
  50.         input1 = input("Number 1> ")
  51.         input2 = input("Number 2> ")
  52.         number1 = float(input1)
  53.         number2 = float(input2)
  54.         result = number1 / number2
  55.         output = str(result)
  56.         print(input1 + " / " + input2 + " = " + output)
  57.  
  58.     elif command == "Volume" or command == "volume":   # Adding code to volume in lines 58-67
  59.         print("lets calculate volume of cylinder with 2 values, ratio and high in cms.")
  60.         input1 = input("ratio > ")
  61.         input2 = input("high > ")
  62.         ratio = float(input1)
  63.         high = float(input2)
  64.         #pi1 = 3.14
  65.         result = math.pi * ratio * ratio * high
  66.         output = str(result)
  67.         print("volume of cylinder = " + output + " cms^3")
  68.  
  69.     elif command == "average" or command == "Average":   # Adding code to average in lines 69-78
  70.         print("lets calculate the average of some numbers!")
  71.         how_many = input("How many numbers > ")
  72.         how_many = int(how_many)
  73.         total = 0
  74.         for number_count in range(how_many):
  75.             number = input("Enter number " + str(number_count + 1) + "> ")
  76.             total = total + int(number)
  77.         result = total/ how_many
  78.         print("the average = " + str(result))
  79.  
  80.     elif command == "shopping" or command == "Shopping":   # Adding code to shopping in lines 80-94
  81.         shopping = []
  82.         how_many = input("how many items of shopping do you have? ")
  83.         how_many = int(how_many)
  84.         total = 0
  85.         for item_number in range (how_many):
  86.             item = input("What is the value of item " + str(item_number + 1) + "? ")
  87.             shopping.append(item)
  88.             total = total + float(item)
  89.         print(shopping)
  90.         print("Total shopping = " + str(total))
  91.  
  92.         for item in shopping:
  93.             print(item)
  94.         print("You have " + str(len(shopping)) + " items in your shopping list" + " and the total shopping is = " + str(total))
  95.  
  96.     else:
  97.         if command == "bye" or command == "Bye":
  98.             finished = True
  99.             print("session finished")
  100.  
  101. print("Bye")
Advertisement
Add Comment
Please, Sign In to add comment