Advertisement
suewalters

4.10 Create a function for totals and averages

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