Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # function to add/subtract/multiply/divide 2 numbers
- def do_calculation(command):
- print ("---" + command + " 2 integers---")
- in_num1 = int(input("Enter 1st number: "))
- in_num2 = int(input("Enter 2nd number: "))
- if command == "add" or command == "Add" :
- operator = "+"
- result = in_num1 + in_num2
- elif command == "subtract" or command == "Subtract":
- operator = "-"
- result = in_num1 - in_num2
- elif command == "multiply" :
- operator = "*"
- result = in_num1 * in_num2
- elif command == "divide":
- operator = "/"
- if in_num2 != 0 :
- result = in_num1 / in_num2
- else:
- print("Cannot divide by 0")
- result = "***"
- print(str(in_num1) + operator + str(in_num2) + "=" + str(result))
- # function to total or average a list of numbers
- def calc_list(command):
- # Capture how many numbers you wish to average/total
- table = []
- table_total = 0
- how_many = input("how many numbers do you want to "+ command + "? ")
- how_many = int(how_many)
- # Loop that many times, capturing the numbers
- for item_number in range(how_many):
- item = input("what is number "+ str(item_number+1)+" ? ")
- item = float(item)
- table.append(item)
- # Keep a running total of the numbers
- table_total = table_total + item
- # display
- print("You have ", how_many, " numbers to " + command)
- for item in table :
- print(item)
- if command == "average" :
- # When the loop has finished divide the total by the number of numbers
- table_ave = table_total/how_many
- print("The average of your numbers is " + str(table_ave))
- elif command == "total":
- print("The total of your numbers is " + str(table_total))
- finished = False
- while finished == False :
- print ("I am Marvin, your personal bot.")
- users_name = input("What is your name? ")
- print ("Welcome " + users_name )
- command = input("How can I help:")
- # Check whether list or 2 integers
- if command in ["add","Add","subtract","Subtract","multiply","divide"]:
- do_calculation(command)
- elif command == "power":
- print ("--Raising a number to a power--")
- in_num1 = int(input("Enter number: "))
- in_num2 = int(input("Enter power: "))
- print(str(in_num1) + "**" + str(in_num2) + "=" + str(in_num1 ** in_num2))
- elif command == "average":
- calc_list(command)
- elif command == "total":
- calc_list(command)
- elif command == "discount":
- # get amount
- net = input("What is the bill amount:")
- net = float(net)
- # get percentage discount
- percen = input("What is the discount percentage (must be less than 100)")
- percen = float(percen)
- # print discount and amount to pay
- discnt = net * (percen/100)
- print("The discount on " + str(net) + " is "+ str(discnt))
- print("Amount to pay :" + str(net-discnt))
- elif command == "bye" :
- finished = True
- # Cater for unknown command
- else:
- print ("I dont understand '" + command + "'")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement