Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def get_float_input(prompt):
- """ Prompt the user for input and convert it to a float, with error handling. """
- while True:
- try:
- value = float(input(prompt))
- if value <= 0:
- print("Please enter a positive number.")
- else:
- return value
- except ValueError:
- print("Invalid input. Please enter a valid number.")
- def get_yes_no_input(prompt):
- """ Prompt the user for a 'yes' or 'no' input, with error handling. """
- while True:
- response = input(prompt).strip().lower()
- if response in ['yes', 'no']:
- return response
- else:
- print("Please enter 'yes' or 'no'.")
- def main():
- # Get dimensions of the room in meters
- x = get_float_input("Enter the width of the room in meters (x): ")
- y = get_float_input("Enter the length of the room in meters (y): ")
- # Calculate the surface area of the room
- surface_area = x * y
- print(f"The surface area of the room is {surface_area:.2f} square meters.")
- # Ask if the user wants to add room height to calculate the volume
- add_height = get_yes_no_input("Do you want to add the height of the room to calculate the volume (yes/no)? ")
- if add_height == "yes":
- z = get_float_input("Enter the height of the room in meters (z): ")
- volume = x * y * z
- print(f"The volume of the room is {volume:.2f} cubic meters.")
- # Ask if the user wants to know how many 2x2x2 cubes can fit in the room
- fit_cubes = get_yes_no_input("Do you want to know how many 2x2x2 m cubes can fit in the room (yes/no)? ")
- if fit_cubes == "yes":
- num_cubes = int(volume // 8) # Each cube is 2x2x2=8 cubic meters
- print(f"You can fit {num_cubes} cubes of 2x2x2 meters in the room.")
- else:
- print("Thank you for using the room calculator.")
- else:
- print("Thank you for using the room calculator.")
- if __name__ == "__main__":
- main()
Add Comment
Please, Sign In to add comment