Guest User

ChatGPT | Python code

a guest
Apr 26th, 2024
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. def get_float_input(prompt):
  2. """ Prompt the user for input and convert it to a float, with error handling. """
  3. while True:
  4. try:
  5. value = float(input(prompt))
  6. if value <= 0:
  7. print("Please enter a positive number.")
  8. else:
  9. return value
  10. except ValueError:
  11. print("Invalid input. Please enter a valid number.")
  12.  
  13. def get_yes_no_input(prompt):
  14. """ Prompt the user for a 'yes' or 'no' input, with error handling. """
  15. while True:
  16. response = input(prompt).strip().lower()
  17. if response in ['yes', 'no']:
  18. return response
  19. else:
  20. print("Please enter 'yes' or 'no'.")
  21.  
  22. def main():
  23. # Get dimensions of the room in meters
  24. x = get_float_input("Enter the width of the room in meters (x): ")
  25. y = get_float_input("Enter the length of the room in meters (y): ")
  26.  
  27. # Calculate the surface area of the room
  28. surface_area = x * y
  29. print(f"The surface area of the room is {surface_area:.2f} square meters.")
  30.  
  31. # Ask if the user wants to add room height to calculate the volume
  32. add_height = get_yes_no_input("Do you want to add the height of the room to calculate the volume (yes/no)? ")
  33. if add_height == "yes":
  34. z = get_float_input("Enter the height of the room in meters (z): ")
  35. volume = x * y * z
  36. print(f"The volume of the room is {volume:.2f} cubic meters.")
  37.  
  38. # Ask if the user wants to know how many 2x2x2 cubes can fit in the room
  39. fit_cubes = get_yes_no_input("Do you want to know how many 2x2x2 m cubes can fit in the room (yes/no)? ")
  40. if fit_cubes == "yes":
  41. num_cubes = int(volume // 8) # Each cube is 2x2x2=8 cubic meters
  42. print(f"You can fit {num_cubes} cubes of 2x2x2 meters in the room.")
  43. else:
  44. print("Thank you for using the room calculator.")
  45. else:
  46. print("Thank you for using the room calculator.")
  47.  
  48. if __name__ == "__main__":
  49. main()
  50.  
Add Comment
Please, Sign In to add comment