Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # this exercise will look at adding commands to the programme
- # using the hash key allows the computer to recognise the text as COMMENTS only
- # and not to be used in the execution of the programme.
- # each new line of comment must be fronted with a # key.
- # in Mu it is shown as greyed out text. In Trinket I think it is green text.
- # the first line of code introduces the bot, Marvin.
- # the second line asks for the user's name.
- # The programme creates a VARIABLE with the answer.
- print("Hello there. My name is Marvin and I'm your personal bot.")
- user_name=input("What's your name?")
- # the third line of code (line 8) instructs the computer to print a welcome message and include the variable called user_name
- print("Hello " + user_name + " and welcome.")
- #the fourth line of code is asking the user to give the computer an instruction that it can only respond to if it has an appropriate command
- command = input("How can I help? >")
- # and from now on the computer is responding to if elif else commands
- if command == "add" or command == "plus":
- print("Let's add some numbers.")
- input1=input("Number1 > ")
- input2=input("Number2 > ")
- number1=int(input1)
- number2=int(input2)
- result=number1+number2
- output=str(result)
- print(input1 + "+" + input2 + "=" + output)
- elif command == "subtract" or command == "take away":
- print("Let's subtract some numbers.")
- input1=input("Number1")
- input2=input("Number2")
- number1=int(input1)
- number2=int(input2)
- result=number1-number2
- output=str(result)
- print(input1 + "-" + input2 + "=" + output)
- # the next line of code instructs the bot to tell the user that it doesn't
- # recognise either of the input answers and to let the user know this then
- # close the conversation
- else:
- print("Sorry I don't understand. Bye.")
- # and the last line is a sign off from the bot
- print ("If that's all, I'll say goodnight.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement