from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
import time
from bs4 import BeautifulSoup
sleep_time = 3
unit_length = -2
welcoming the user to the laptop calculator and asking them to input their desired specifications.
print("Laptop Calculator")
print("Enter Below the Specifications You Require")
print(" ")
This is a function that takes a prompt and a list of options as inputs, and returns the user's choice from the list.
It loops until the user inputs a valid option,
and prints a message indicating their choice or notifying them of an invalid choice.
This is a while loop which asks the user to choose the minimum and maximum memory values from the list
using the get choice function
it converts the users input into an integer
if the maximum memory value is bigger than the minimum memory value it will break the loop and ask you to try again
this stores the users input in two variables memory_size_min and memory_size_max
memory_list = ["8GB", "16GB", "32GB", "64GB"]
while True:
memory_min_choice = get_choice("Please choose the minimum value for one of the following memory options:",
memory_list)
memory_max_choice = get_choice("Please choose the maximum value from one of the following memory options:",
memory_list)
code
memory_size_min = int(memory_min_choice[:unit_length])
memory_size_max = int(memory_max_choice[:unit_length])
if memory_size_min <= memory_size_max:
break
print("Invalid choice. The minimum value cannot be greater than the maximum value. Please try again.")
This is a while loop which asks the user to choose the minimum and maximum screen size values from the list
using the get choice function
it converts the users input into an integer
if the maximum screen size value is bigger than the minimum screen size value it will break the loop and restart
this stores the users input in two variables screen_size_min and screen_size_max
screen_list = ["10", "12", "14", "16", "18"]
while True:
screen_min_choice = get_choice("Please choose the minimum value for one of the following screen size options "
"(inches):", screen_list)
screen_max_choice = get_choice("Please choose the maximum value for one of the following screen size options"
" (inches):", screen_list)
Assistant
code
screen_size_min = int(screen_min_choice)
screen_size_max = int(screen_max_choice)
if screen_size_min <= screen_size_max:
break
print("Invalid choice. The minimum value cannot be greater than the maximum value. Please try again.")
This is a while loop which asks the user to choose the minimum and maximum SSD values from the list
using the get_choice function
it converts the users input into an integer
if the maximum SSD value is bigger than the minimum SSD value it will break the loop and restart
this stores the users input in two variables ssd_size_min and ssd_size_max
ssd_list = ["32GB", "64GB", "256GB", "512GB", "1000GB"]
while True:
ssd_min_choice = get_choice("Please choose the minimum value for one of the following SSD storage options:",
ssd_list)
ssd_max_choice = get_choice("Please choose the maximum value one of the following SSD storage options:",
ssd_list)
code
ssd_size_min = int(ssd_min_choice[:unit_length])
ssd_size_max = int(ssd_max_choice[:unit_length])
if ssd_size_min <= ssd_size_max:
break
print("Invalid choice. The minimum value cannot be greater than the maximum value. Please try again.")
using the get_choice function it asks the user to choose an operating system and saves it in the variable system_list
system_list = ["WINDOWS", "MACOS"]
system_choice = get_choice("Please choose one of the operating system following options:",
system_list)
Check the system choice and select the corresponding OS from the dropdown
if system_choice == "MACOS":
dropdown = driver.find_elements(By.CLASS_NAME, "ui-dropdownchecklist-selector")
dropdown[4].click()
code
# Find and click the checkbox for macOS
mac_checkbox_element = driver.find_element(By.ID, "ddcl-filter_22[]-i2")
mac_checkbox_element.click()
else:
# For non-macOS systems, click on the OS dropdown and select the "Windows" option
dropdown = driver.find_elements(By.CLASS_NAME, "ui-dropdownchecklist-selector")
dropdown[4].click()
code
# Find and click the checkboxes for each Windows version in the "windowsIds" list
windows_ids = ["2391", "421", "190", "477", "819", "7546", "2012", "7546", "3645", "3186", "3278", "4246", "7544",
"7548", "7545", "7737"]
for windows in windows_ids:
windows_checkbox = driver.find_element(By.CSS_SELECTOR, f'input[id*="ddcl-filter_22"][value="{windows}"]')
windows_checkbox.click()
This code section is used to adjust the RAM slider on the webpage
go through all the laptops and prices on the page and print
for laptop, price in zip(laptops, prices):
print(laptop.text, price.text)
print()
input("Press enter to close the browser...")
driver.quit()
ChatGPT
The code seems to be well-structured and follows Python's PEP 8 style guide. It also uses appropriate variable names and includes comments that describe the purpose of each section of code.
However, I cannot fully evaluate the code's overall quality without additional context or a specific set of criteria for what constitutes "excellence." In addition, there may be other factors to consider beyond the code itself, such as how it fits into a larger project or how it performs in terms of efficiency and scalability.
Here are a few suggestions for improving the code:
Use functions: Break the code into smaller, reusable functions that handle specific tasks. This can make the code easier to read and maintain.
Use a dictionary for laptop options: Instead of using separate lists for memory, SSD, and screen size choices, consider using a dictionary with keys as the option names and values being lists of available choices. This can simplify the code for user prompts.
Exceptions handling: Add try-except blocks around webdriver and other parts of the code that may raise exceptions. This will help provide a more graceful exit strategy with meaningful error messages instead of abrupt crashes.
Add user input validation: Before using user inputs to filter the laptop search, validate and sanitize them to prevent any possible errors while interacting with the web page.
Here's an example of how you might refactor the code using functions and the other improvements mentioned above:
python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time
from bs4 import BeautifulSoup
def welcome_message():
print("Laptop Calculator")
print("Enter Below the Specifications You Require")
print(" ")
def get_choice(prompt, options):
while True:
choice = input(prompt + " " + ", ".join(options) + " ").upper()
if choice in options:
print("You chose", choice)
return choice
else:
print("Invalid choice. Please try again.")
def get_memory_choice(memory_list):
while True:
memory_min_choice = get_choice(
"Please choose the minimum value for one of the following memory options:", memory_list)
memory_max_choice = get_choice(
"Please choose the maximum value from one of the following memory options:", memory_list)
memory_size_min = int(memory_min_choice[:-2])
memory_size_max = int(memory_max_choice[:-2])
if memory_size_min <= memory_size_max:
break
print("Invalid choice. The minimum value cannot be greater than the maximum value. Please try again.")
return memory_size_min, memory_size_max
def get_screen_choice(screen_list):
while True:
screen_min_choice = get_choice(
"Please choose the minimum value for one of the following screen size options (inches):", screen_list)
screen_max_choice = get_choice(
"Please choose the maximum value for one of the following screen size options (inches):", screen_list)
screen_size_min = int(screen_min_choice)
screen_size_max = int(screen_max_choice)
if screen_size_min <= screen_size_max:
break
print("Invalid choice. The minimum value cannot be greater than the maximum value. Please try again.")
return screen_size_min, screen_size_max
def get_ssd_choice(ssd_list):
while True:
ssd_min_choice = get_choice(
"Please choose the minimum value for one of the following SSD storage options:", ssd_list)
ssd_max_choice = get_choice(
"Please choose the maximum value one of the following SSD storage options:", ssd_list)
ssd_size_min = int(ssd_min_choice[:-2])
ssd_size_max = int(ssd_max_choice[:-2])
if ssd_size_min <= ssd_size_max:
break
print("Invalid choice. The minimum value cannot be greater than the maximum value. Please try again.")
return ssd_size_min, ssd_size_max
def main():
welcome_message()
memory_list = ["8GB", "16GB", "32GB", "64GB"]
screen_list = ["10", "12", "14", "16", "18"]
ssd_list = ["32GB", "64GB", "256GB", "512GB", "1000GB"]
memory_size_min, memory_size_max = get_memory_choice(memory_list)
screen_size_min, screen_size_max = get_screen_choice(screen_list)
ssd_size_min, ssd_size_max = get_ssd_choice(ssd_list)
system_list = ["WINDOWS", "MACOS"]
system_choice = get_choice("Please choose one of the operating system following options:", system_list)
# Continue with the rest of the code...
if __name__ == '__main__':
main()
By following these suggestions, you can further improve the readability, maintainability, and robustness of your code.