Advertisement
suewalters

4.9 reusing code

May 13th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. finished = False
  2. def do_calculation():
  3. print("Let us " + command + " some numbers")
  4. input1=input("Number 1> ")
  5. input2=input("Number 2> ")
  6. number1=int(input1)
  7. number2=int(input2)
  8. if command=="add":
  9. result=number1+number2
  10. operator=" + "
  11. elif command=="subtract":
  12. result=number1-number2
  13. operator=" - "
  14. else:
  15. print(command+" is not a valid answer. I do not know if I should add, subtract or calculate an average. Please Try again.")
  16. output=str(result)
  17. print(input1+operator+input2+" = "+output)
  18.  
  19. while finished == False:
  20. print("Hi, I am the bot that Sue created.")
  21. print("Let's get started.")
  22. users_name=input("What is your name? ")
  23. print("Welcome "+users_name)
  24. command=input("Please type add, subtract, or mean to calculate the average. Type bye if you would like to leave the program.>")
  25. if command=="bye":
  26. finished=True
  27. elif command=="mean":
  28. how_many=input("How Many Numbers Do You Want to Average?>")
  29. how_many=int(how_many)
  30. total=0
  31. for number_count in range(how_many):
  32. number=input("Enter number "+str(number_count)+"> ")
  33. total = total + int(number)
  34. result=total/how_many
  35. print("The average = "+str(result))
  36. else:
  37. do_calculation()
  38.  
  39. if finished==False:
  40. shopping=[]
  41. count=0
  42. how_many=input(users_name+", how many items do you have on your shopping list? ")
  43. how_many=int(how_many)
  44. for item_number in range(how_many):
  45. item=input("What is the item number "+str(item_number)+" ? ")
  46. shopping.append(item)
  47. for item in shopping:
  48. print(item)
  49. count=count+1
  50. print(count)
  51. print (len(shopping))
  52. strcount=str(count)
  53. print (users_name+", you have "+strcount+" items in your shopping list.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement