Advertisement
Guest User

Calculator

a guest
Nov 20th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.79 KB | None | 0 0
  1. import math
  2. finished = False
  3. # Asks the user what its name is and stores it as a value
  4. # Asks user for their name and stores it under user_name
  5. user_name = input("What is your name?>")
  6. bots_name = "Mansoor"  # Name of the bot
  7. print("Hi, I'm " + bots_name + ", your personal bot.")
  8. print("Welcome " + user_name + "!!")
  9. while not finished:
  10.     # This shows the user the options available for the bot to perform
  11.     print("What would you like to do?")
  12.     print("1 - Add 2 numbers")
  13.     print("2 - Subtract 2 numbers")
  14.     print("3 - Calculate the area of a triangle")
  15.     print("4 - Caculate the area of a square")
  16.     print("5 - Multiply 2 numbers")
  17.     print("6 - Divide 2 numbers")
  18.     print("7 - Calculate the average of a set of numbers")
  19.     print("8 - Generate a shopping list")
  20.     print("9 - Calculate Area of a circle")
  21.     print("10 - Find the roots of a quadratic function")
  22.     print("11 - End program")
  23.     value = input("Value> ")
  24.     print(" ")
  25.    
  26.     # If the person picks 1, then they trigger the addition command
  27.     if value == "1" or value == "one" or value == "One":
  28.         print("Let's add some numbers")
  29.         input1 = input("Number 1> ")
  30.         input2 = input("Number 2> ")
  31.         number1 = int(input1)
  32.         number2 = int(input2)
  33.         result = number1 + number2
  34.         addoutput = str(result)
  35.         print(input1 + " + " + input2 + " = " + addoutput)
  36.         print(" ")
  37.  
  38.     # If the person picks 2, then they trigger the subtraction command
  39.     elif value == "2":
  40.         print("Let's subtract some numbers")
  41.         input1 = input("Number 1> ")
  42.         input2 = input("Number 2> ")
  43.         number1 = int(input1)
  44.         number2 = int(input2)
  45.         result = number1 - number2
  46.         suboutput = str(result)
  47.         print(input1 + " - " + input2 + " = " + suboutput)
  48.         print(" ")
  49.  
  50.     # If the person picks 3, then they calculate the area of a triangle
  51.     elif value == "3" or value == "three" or value == "Three":
  52.         print("Let's calculate the area of a triangle")  # (b*h)/2
  53.         base = input("Base (in cm)> ")
  54.         height = input("Height (in cm)> ")
  55.         numbase = int(base)
  56.         numheight = int(height)
  57.         result = numbase * numheight/2
  58.         trioutput = str(result)
  59.         print("Area of the triangle = " + trioutput + "cm\u00b2")
  60.         print(" ")
  61.  
  62.     # If the person picks 4, then they can calculate a square number
  63.     elif value == "4" or value == "four" or value == "Four":
  64.         print("Let's calculate area of a square")  # n^2
  65.         length = input("length (in cm)>")
  66.         numx = int(length)
  67.         result = numx * numx
  68.         sqoutput = str(result)
  69.         print("Area of this square is = " + sqoutput + "cm\u00b2")
  70.         print(" ")
  71.  
  72.     # If the person picks 5, then they multiply two numbers
  73.     elif value == "5" or value == "five" or value == "Five":
  74.         print("Let's multiply some numbers")
  75.         input1 = input("Number 1> ")
  76.         input2 = input("Number 2> ")
  77.         number1 = int(input1)
  78.         number2 = int(input2)
  79.         result = number1 * number2
  80.         multoutput = str(result)
  81.         print(input1 + " x " + input2 + " = " + multoutput)
  82.         print(" ")
  83.  
  84.     # If the person picks 6, then they divide numbers
  85.     elif value == "6" or value == "six" or value == "Six":
  86.         print("Let's divide some numbers")
  87.         input1 = input("Number 1> ")
  88.         input2 = input("Number 2> ")
  89.         number1 = int(input1)
  90.         number2 = int(input2)
  91.         if input2 == "0":
  92.             print("Invalid solution")
  93.             print(" ")
  94.         else:
  95.             result = number1 / number2
  96.             divoutput = str(result)
  97.             print(input1 + " / " + input2 + " = " + divoutput)
  98.             print(" ")
  99.  
  100.     # If the person picks 7, this means they are able
  101.     # to calculate averages of a set of numbers
  102.     elif value == "7" or value == "seven" or value == "Seven":
  103.         how_many = input("How many numbers> ")
  104.         how_many = int(how_many)
  105.         total = 0
  106.  
  107.         for number_count in range(how_many):
  108.             number = input("Enter number " + str(number_count) + "> ")
  109.             total = total + int(number)
  110.  
  111.         result = total/how_many
  112.         print("The average = " + str(result))
  113.         print(" ")
  114.  
  115.     # If the person picks 8, it will let you generate a shopping list
  116.     elif value == "8" or value == "eight" or value == "Eight":
  117.         shopping = []
  118.         shopping_cost = []
  119.         count = 0
  120.         shopping_total = 0
  121.  
  122.         how_many = input("How many items of shopping do you need?> ")
  123.         how_many = int(how_many)
  124.  
  125.         for item_number in range(how_many):
  126.             item = input("What is item number " + str(item_number + 1) + " ?> ")
  127.             shopping.append(item)
  128.             count = count + 1
  129.             price = input("How much does item number " + str(item_number + 1) + " cost?> £")
  130.             fltprice = float(price)
  131.             shopping_cost.append(price)
  132.             shopping_total += fltprice
  133.  
  134.         print("Shopping list:")
  135.         for item in shopping:
  136.             print(item)
  137.  
  138.         strcount = str(count)
  139.  
  140.         if count == 1:
  141.             print("There is only 1 thing you need to get")
  142.         else:
  143.             print("There are " + strcount + " things you need to get")
  144.  
  145.         strshopping_total = str(shopping_total)
  146.         print("Shopping total = £" + strshopping_total)
  147.         print(" ")
  148.    
  149.     # If the person picks 9 it calculates the area of a circle using 3.142
  150.     elif value == "9" or value == "nine" or value == "Nine":
  151.         print("Let's calculate the area of a circle")
  152.         rd = input("What is the radius of the circle? (in cm)> ")
  153.         radius = int(rd)
  154.         CircArea = math.pi * radius * radius
  155.         StrCirc = str(CircArea)
  156.         print("The area of this circle is " + StrCirc + "cm\u00b2" )
  157.         print(" ")
  158.    
  159.     elif value == "10" or value == "ten" or value == "Ten":
  160.         print("Let's find the roots of a quadratric"
  161.         a = input("What is your coefficient of x\u00b2?> ")
  162.         b = input("What is your coefficient of x?> ")
  163.         c = input("What is the integer value?> ")
  164.         inta = int(a)
  165.         intb = int(b)
  166.         intc = int(c)
  167.        
  168.         Discriminant = (intb*intb)-4*inta*intc
  169.        
  170.         if Discriminant > 0:
  171.             print("There are 2 real solutions")
  172.             print("And they are: ")
  173.            
  174.             PosRoot = ( (-intb) + math.sqrt(Discriminant) ) / ( 2 * inta )
  175.             NegRoot = ( (-intb) - math.sqrt(Discriminant) ) / ( 2 * inta )
  176.             strPosRoot = str(PosRoot)
  177.             strNegRoot = str(NegRoot)
  178.            
  179.             print("Root 1> " + strPosRoot)
  180.             print("Root 2> " + strNegRoot)
  181.             print(" ")
  182.        
  183.         elif Discriminant == 0:
  184.             print("There is only 1 real solution")
  185.             print("And they are: ")
  186.            
  187.             SingRoot = ( (-intb) + math.sqrt(Discriminant) ) / ( 2 * inta )
  188.             strSingRoot = str(SingRoot)
  189.            
  190.             print("Root - " + strSingRoot)
  191.             print(" ")
  192.            
  193.         else:
  194.             print("There are no real solutions")
  195.             print(" ")
  196.    
  197.     # If the person picks 11 the program ends
  198.     elif value == "11" or value == "eleven" or value == "Eleven":
  199.         finished = True
  200.         print("Bye " + user_name)
  201.         print(" ")
  202.  
  203.     # What the person entered was invalid
  204.     else:
  205.         print("I'm sorry, but that isn't a valid value")
  206.         print("Please pick a number between 1 and 10")
  207.         print(" ")
  208.     # If the person picks 4, then they can calculate a square number
  209.     elif value == "4" or value == "four" or value == "Four":
  210.         print("Let's calculate area of a square")  # n^2
  211.         length = input("length>")
  212.         numx = int(length)
  213.         result = numx * numx
  214.         sqoutput = str(result)
  215.         print("Area of this square is = " + sqoutput)
  216.  
  217.     # If the person picks 5, then they multiply two numbers
  218.     elif value == "5" or value == "five" or value == "Five":
  219.         print("Let's multiply some numbers")
  220.         input1 = input("Number 1> ")
  221.         input2 = input("Number 2> ")
  222.         number1 = int(input1)
  223.         number2 = int(input2)
  224.         result = number1 * number2
  225.         multoutput = str(result)
  226.         print(input1 + " x " + input2 + " = " + multoutput)
  227.  
  228.     # If the person picks 6, then they divide numbers
  229.     elif value == "6" or value == "six" or value == "Six":
  230.         print("Let's divide some numbers")
  231.         input1 = input("Number 1> ")
  232.         input2 = input("Number 2> ")
  233.         number1 = int(input1)
  234.         number2 = int(input2)
  235.         if input2 == "0":
  236.             print("Invalid solution")
  237.         else:
  238.             result = number1 / number2
  239.             divoutput = str(result)
  240.             print(input1 + " / " + input2 + " = " + divoutput)
  241.  
  242.     # If the person picks 7, this means they are able
  243.     # to calculate averages of a set of numbers
  244.     elif value == "7" or value == "seven" or value == "Seven":
  245.         how_many = input("How many numbers> ")
  246.         how_many = int(how_many)
  247.         total = 0
  248.  
  249.         for number_count in range(how_many):
  250.             number = input("Enter number " + str(number_count) + "> ")
  251.             total = total + int(number)
  252.  
  253.         result = total/how_many
  254.         print("The average = " + str(result))
  255.  
  256.     # If the person picks 8, it will let you generate a shopping list
  257.     elif value == "8" or value == "eight" or value == "Eight":
  258.         shopping = []
  259.         shopping_cost = []
  260.         count = 0
  261.         shopping_total = 0
  262.  
  263.         how_many = input("How many items of shopping do you need?> ")
  264.         how_many = int(how_many)
  265.  
  266.         for item_number in range(how_many):
  267.             item = input("What is item number " + str(item_number + 1) + " ?> ")
  268.             shopping.append(item)
  269.             count = count + 1
  270.             price = input("How much does item number " + str(item_number + 1) + " cost?> £")
  271.             fltprice = float(price)
  272.             shopping_cost.append(price)
  273.             shopping_total += fltprice
  274.  
  275.         print("Shopping list:")
  276.         for item in shopping:
  277.             print(item)
  278.  
  279.         strcount = str(count)
  280.  
  281.         if count == 1:
  282.             print("There is only 1 thing you need to get")
  283.         else:
  284.             print("There are " + strcount + " things you need to get")
  285.  
  286.         strshopping_total = str(shopping_total)
  287.         print("Shopping total = £" + strshopping_total)
  288.  
  289.     elif value == "9" or value == "nine" or value == "Nine":
  290.         finished = True
  291.         print("Bye " + user_name)
  292.  
  293.     # What the person entered was invalid
  294.     else:
  295.         print("I'm sorry, but that isn't a valid value")
  296.         print("Please pick a number between 1 and 9")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement