Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Programming 101: An introduction to Python for Educators
- # on Future Learn, and Raspberry Pi Foundation
- # In this program we are creating a bot more intelligent than its creator.
- # option two: we will use functions in this version
- # a function to calculate: add, subtract, multiply, divide
- def do_calculation():
- print("lets " + command + " some numbers")
- input1 = input("Number 1> ")
- input2 = input("Number 2> ")
- number1 = int(input1)
- number2 = int(input2)
- if command == "add":
- result = number1 + number2
- operator = " + "
- elif command == "subtract":
- result = number1 - number2
- operator = " - "
- elif command == "multiply":
- result = number1 * number2
- operator = " * "
- elif command == "divide":
- if number2 != 0:
- result = number1/number2
- operator = " / "
- if command == "divide" and number2 == 0:
- print("Warning: Undefined, division by zero is undefined")
- else:
- output = str(result)
- print("Answer: " + input1 + operator + input2 + " = " + output)
- # a function to calculate: total, average
- def do_statistics():
- how_many = input("How many numbers> ")
- how_many = int(how_many)
- total = 0
- for number_count in range(how_many):
- number = input("Enter number " + str(number_count) + "> ")
- total = total + int(number)
- if command == "total":
- result = total
- elif command == "average":
- result = total / how_many
- print("Answer: " + command + " " + str(result))
- finished = False
- while finished == False:
- print("**********************************************")
- print('Hi, I am Marvin, your personal bot.')
- # ask the user to enter their name
- # users_name = input("What is your name? ")
- # print a message including the name
- # print('Welcome ' + users_name )
- # ask for the user's wishes and fantacies
- Things_I_Do=['add','subtract','multiply','divide','total','average','shop','bye']
- print("These are the tasks I can perform: ", Things_I_Do)
- command = input("How can I help? ")
- if command == "add":
- do_calculation()
- elif command == "subtract":
- do_calculation()
- elif command == "multiply":
- do_calculation()
- elif command == "divide":
- do_calculation()
- elif command == "total":
- do_statistics()
- elif command == "average":
- do_statistics()
- elif command == "shop":
- shopping = []
- how_many = input("how many items of shopping do you have? ")
- how_many = int(how_many)
- for item_number in range(how_many):
- item = input("what is item number " + str(item_number+1) + "? ")
- shopping.append(item)
- print("Shopping List: ", shopping)
- count = 0
- for item in shopping:
- print(item)
- count = count +1
- print("you have ", count, " items in your shopping list")
- elif command == "bye":
- finished = True
- else:
- print("sorry I dont understand")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement