Exception handler to check if inline script for variable worked

2024/11/18 5:34:49

I need to add exception handling that considers if line 7 fails because there is no intersection between the query and array brands. I'm new to using exception handlers and would appreciate any advice or solutions.

I have written an example structure for exception handling, but I am not certain whether it will work.

brands = ["apple", "android", "windows"]query = input("Enter your query: ").lower()
brand = brandSelector(query)
print(brand)def brandSelector(query):try: brand = set(brands).intersection(query.split())brand = ', '.join(brand)return brandexcept ValueError:print("Brand does not exist")# Redirect to main script to enter correct brand in query
Answer

This is not the best way to do it, but it is a way.

def brandSelector(query):try:brand = set(brands).intersection(query.split())brand = ', '.join(brand)return brandexcept ValueError:print("Brand does not exist")query = input("Enter your query: ").lower()brandSelector(query)brands = ["apple", "android", "windows"]    
query = input("Enter your query: ").lower()
brand = brandSelector(query)
print(brand)

Your function is now recursive since it includes a call to itself. What happens is that if the try throws an error, the except gets triggered where the user is prompted to redefine the query. The function is then reran.


If no error is thrown by the intersection() but instead an empty container is returned, you can do the following:

def brandSelector(query):brand = set(brands).intersection(query.split())brand = ', '.join(brand)return brandbrands = ["apple", "android", "windows"]
brand = None
while not brand:query = input("Enter your query: ").lower()brand = brandSelector(query)
print(brand)

Which looks a lot like Tuan333's answer.

https://en.xdnf.cn/q/118708.html

Related Q&A

Parameter list with single argument

When testing Python parameter list with a single argument, I found some weird behavior with print.>>> def hi(*x): ... print(x) ... >>> hi() () >>> hi(1,2) (1, 2) >>…

Scatter plot of values in pandas dataframe

I have a pandas dataframe in the following format. I am trying to plot this data based on ClusterAssigned, with probably different colors for 0 and 1. Distance ClusterAssigned23 135 120 …

String Delimiter in Python

I want to do split a string using "},{" as the delimiter. I have tried various things but none of them work.string="2,1,6,4,5,1},{8,1,4,9,6,6,7,0},{6,1,2,3,9},{2,3,5,4,3 "Split it i…

Wrong encoding of email attachment

I have a python 2.7 script running on windows. It logs in gmail, checks for new e-mails and attachments:#!/usr/bin/env python # -*- coding: utf-8 -*-file_types = ["pdf", "doc", &quo…

Blank lines in txt files in Python

I want to write sensor values to a text file with Python. All is working fine but one thing; in the text file, there are blank lines between each value. Its really annoying because I cant put the value…

Python 3 - decode spectroscopy data (Base64, IEEE754)

Im a chemist and working with spectroscopic data that was stored as a list (501 pairs of X,Y data) of Base64-encoded floating point values according to IEEE754.I tried to get an array of X, Y data to w…

Fill new column by following conditions listed in a dictionary [duplicate]

This question already has an answer here:Insert data to new column based on conditions given in dictionary(1 answer)Closed 2 years ago.I have the dictionary specifying the value the row should take if …

Pandas - Splitting dataframe into multiple excel workbooks by column value

Im new to pandas. I have a large excel file, what I’m trying to do is split the data frame after manipulation into multiple excel workbooks. There is more or less 400 vendors and I would like each To …

Create Sections in Python

I am newbie to Python. I have large file with repetitive string through the logsExample:abc def efg gjk abc def efg gjk abc def efg gjk abc def efg gjkExpected Result--------------------Section1-------…

Process CSV files in Python - Zero Imports/No Libraries

I have CSV example like this ID,TASK1,TASK2,QUIZ1,QUIZ2 11061,50,75,50,78 11062,70,80,60,50 11063,60,75,77,79 11064,52,85,50,80 11065,70,85,50,80how do i get the Max, Min and Avg on specific Column? i…