Jed1

try- Except python Soln

Sep 27th, 2019
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.50 KB | None | 0 0
  1. class bedRoom:
  2. temp1 = 0
  3. pur1 = 0
  4. bedwash = 0
  5.  
  6.  
  7. def __init__(self, temp1, pur1, bedwash):
  8. self.temp1 = temp1
  9. self.pur1 = pur1
  10. self.bedwash = bedwash
  11.  
  12.  
  13.  
  14. def getdatpass(self):
  15. print("request is being processed ")
  16.  
  17.  
  18. def functiondatTemp(self):
  19. if self.temp1 == 27:
  20. print("Bedroom has ideal temperature ")
  21. elif self.temp1 >= 28 or self.temp1 <= 26:
  22. print("Bedroom Temperature is not ideal ,either too low or too cold. ")
  23. print("Please to adjust the temperature to the optimum temperature ")
  24.  
  25. def functiondatPur(self):
  26.  
  27. if self.pur1 == "neat":
  28. print("Bedroom is hygienic ")
  29. elif self.pur1 == "dirty":
  30. print("Bedroom Temperature is not hygienic ")
  31. print("Please to clean the room ")
  32.  
  33. # assign the input which is neither neat nor a not neat to a variable an raise an exception.
  34.  
  35.  
  36.  
  37.  
  38.  
  39. def functiondatBedwash(self):
  40. if self.bedwash == 27:
  41. print("Bedroom has optimum water level ")
  42. elif self.bedwash >= 28 or self.bedwash<= 26:
  43. print("Bedroom water is not ideal ,either there is no water or tap is overflowing. ")
  44. print("Please to adjust the water pump to have optimum water level ");
  45.  
  46. def chooSefunction(self):
  47.  
  48. print("enter A for temperature,")
  49. print("B for purity assessment ")
  50. print("C for bedroom water level")
  51. print("D for all options check ")
  52. print("E for Exit or move to end class or return nothing and move on to next class maybe other Rooms")
  53. try:
  54. bedroMspefs = input(
  55. " Enter choice in Caps over here ")
  56. # exception to function can be continued here
  57. # where the error handler can handle any odd choice and and re run the function chooSefunction.
  58.  
  59. #this function acts behind the scene to check if a string is been input, if its not it will throw
  60. #an exception .
  61.  
  62. reaLcheck=bedRoom +"chicken"
  63. if bedroMspefs == "A":
  64. # check indention in future whether it affects the class
  65. overalldat1.functiondatTemp()
  66. elif bedroMspefs == "B":
  67. overalldat1.functiondatPur()
  68. elif bedroMspefs == "C":
  69. overalldat1.functiondatBedwash()
  70. elif bedroMspefs == "D":
  71. overalldat1.functiondatTemp()
  72. overalldat1.functiondatPur()
  73. overalldat1.functiondatBedwash()
  74.  
  75. elif bedroMspefs == "E":
  76. print("we will be moving on to the the rooms since you have chosen to exit to other choices ")
  77.  
  78. except ValueError:
  79. print ("you have an invalid character which is not a string ")
  80. print("Enter the right data")
  81.  
  82. except TypeError:
  83. print("you have an invalid character which is not a string ")
  84. print("Enter the right data")
  85.  
  86.  
  87. finally:
  88. if bedroMspefs != "A" or bedroMspefs != "B" or bedroMspefs != "C" or bedroMspefs != "D":
  89.  
  90. # this will function will be run finally after the variable whether the reaLcheck has escaped the exception or not
  91. # this will work because of the code says "finally".
  92. overalldat1.chooSefunction()
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99. # Error handling is first thing done
  100. # can be be done on the alphabet choice whether if choice is not A or B or C or D then the error should be caught
  101.  
  102.  
  103.  
  104.  
  105.  
  106. # and print invalid choice and return user to the choose a right option or alphabet either A or B or C ... also how can i change
  107. #
  108. # return the function which brings back the enter values side ;
  109.  
  110. # i will have to use try statements to catch errors here if user chooses an wrong alphabet he have to
  111. # to choose again until he has choosing the right one
  112.  
  113.  
  114.  
  115.  
  116. # creating instance of objects
  117.  
  118.  
  119. # calling functions and initializing class to start
  120. temp1 = int(input("enter the temp"))
  121. pur1 = input("enter the purity ,if bedroom is clean,Enter neat in small caps if not enter dirty ")
  122. bedwash = int(input("enter the bedwash"));
  123.  
  124. overalldat1 = bedRoom(temp1, pur1, bedwash);
  125.  
  126. overalldat1.chooSefunction()
  127.  
  128.  
  129.  
  130. #
Advertisement
Add Comment
Please, Sign In to add comment