Python Forum
Have function reject bad argument when argument is called by another function?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Have function reject bad argument when argument is called by another function?
#1
I am creating a functions that calls upon two other functions: hal(a) and acc(b).
x = {"F": "Fluorine", "Cl": "Chlorine"}
y = {"O": "Oxygen"}

def hal(a):
  for halogens in x:
    if halogens == a:
      print(x[a])
  return(x[a])  
  

def acc(b):
  for acceptors in y:
    if acceptors == b:
      print(y[b])
  return y[b]
 



def is_halogen_bond(a, b): 
  hal(a)
  acc(b)

is_halogen_bond("F", "O")
I want the functions hal(a) and acc(b) to prompt for halogen and acceptor if a and b in is_halogen_bond(a,b) are not apart of the lists x and y.

I am really struggling here Huh Huh Huh Huh ! Any help would be appreciated!

THANK YOU!
Reply
#2
I'm not sure if I understood what you want, but if you want to check for a key in a dict, you can do this:

x = {"F": "Fluorine", "Cl": "Chlorine"}
y = {"O": "Oxygen"}


def check_key(d, key):
    if key in d:
        print(d[key])
        return True
    return False


def is_halogen_bond(a, b):
    return check_key(x, a) and check_key(y, b)


print(is_halogen_bond("F", "O"))
Reply
#3
Thank you I will take a look.

I will clarify what I meant if this does not solve the issue.

x = {"F": "Fluorine", "Cl": "Chlorine"}
y = {"O": "Oxygen"}
 
def hal(a):
  for halogens in x:
    if halogens == a:
      print(x[a])
  return(x[a])  
   
 
def acc(b):
  for acceptors in y:
    if acceptors == b:
      print(y[b])
  return y[b]
  
def is_halogen_bond(halogen, acceptor ): 
  hal(a)
  acc(b)
 
is_halogen_bond("F", "O")
On the code that I have I want is
 is_halogen_bond("F", "O") 
to print Fluorine and Oxygen, which it does.

I get a "KeyError" every time that "halogen" or "acceptor" is put inside of the arguments. Perhaps my failed code would be a better way to illustrate what I am speaking of . . .

Error:
Traceback (most recent call last): File "halogen.py", line 24, in <module> is_halogen_bond("B", "O") File "halogen.py", line 21, in is_halogen_bond hal(halogen) File "halogen.py", line 8, in hal return(x[a]) KeyError: 'B'
To resolve this issue I attempted a while loop and all loops under the sun and I failed. What I want my code to do from this point is then print something like this if the halogen argument produces a key error:
print("Error, not a halogen.")
and then prompt the user to enter a halogen that is in the list x. I want the same to apply for acceptors with the list y.

I know loops are involved in here but I am struggling to create one correctly.

Thanks for your time!

Sorry, I made a mistake...

I meant " I get a "KeyError" every time a "halogen" or "acceptor" is put inside of the arguments that is not listed in x or y. Perhaps my failed code would be a better way to illustrate what I am speaking of . . ."
Reply
#4
I am supposed to make a program for my internship that identifies halogen bonds but I have been having trouble.

Here is the code:
x = {"F": "Fluorine", "Cl": "Chlorine"}
y = {"O": "Oxygen"}



def hal(a, key):
  if key in a:
    print(x[key])
  return x[key]  
  

def acc(b, key):
  if key in b:
    print(y[key])
  return y[key] 

def is_halogen_bond(halogen, acceptor): 
  hal(x, halogen)
  acc(y, acceptor)


is_halogen_bond("Br", "O")
My code is below and it is not complete but I have been trying to get past a key error that is generated when I put in "Br" for halogen on line 22. I know that it will generate a key error and that was my point.

Here is the error:
Error:
Traceback (most recent call last): File "halogen.py", line 22, in <module> is_halogen_bond("Br", "O") File "halogen.py", line 18, in is_halogen_bond hal(x, halogen) File "halogen.py", line 9, in hal return x[key] KeyError: 'Br'
To resolve this, I want to be able to create a loop that will prompt the user to enter a key that is in the dictionary x until a key from the dictionary x is entered. Then I want it to print the value for that key. I do not know what steps I should take.

I am really new to programming and any support is greatly appreciated.

PS: If the function arguments are "F" and "O", the code does what I want. I want it to be able to handle exceptions.
Reply
#5
Please, use the same thread for same issues.

Check this implementation:

x = {"F": "Fluorine", "Cl": "Chlorine"}
y = {"O": "Oxygen"}


def hal(target_dict, key):
    try:
        # This will raise KeyError if not found
        print(target_dict[key])
    except KeyError:
        print(f'{key}: KeyError')


def acc(target_dict, key):
    try:
        # This will raise KeyError if not found
        print(target_dict[key])
    except KeyError:
        print(f'{key}: KeyError')


def is_halogen_bond(halogen, acceptor):
    hal(x, halogen)
    acc(y, acceptor)


user_key1 = input('Enter key 1: ')
user_key2 = input('Enter key 2: ')

while user_key1 != 'exit' and user_key2 != 'exit':
    is_halogen_bond(user_key1, user_key2)
    user_key1 = input('Enter key 1: ')
    user_key2 = input('Enter key 2: ')
Reply
#6
Thank you for your help!

And I will remember that for future reference.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Django "Unexpected Keyword Argument 'min_value'" AstralWeeks 0 100 Mar-27-2024, 04:56 AM
Last Post: AstralWeeks
  File is not being created with 'w' argument CAD79 3 321 Mar-14-2024, 12:05 PM
Last Post: snippsat
  The function of double underscore back and front in a class function name? Pedroski55 9 563 Feb-19-2024, 03:51 PM
Last Post: deanhystad
  __init__() got multiple values for argument 'schema' dawid294 4 1,891 Jan-03-2024, 09:42 AM
Last Post: buran
  mutable argument in function definition akbarza 1 426 Dec-15-2023, 02:00 PM
Last Post: deanhystad
  Multiple variable inputs when only one is called for ChrisDall 2 449 Oct-20-2023, 07:43 PM
Last Post: deanhystad
  Strange argument count error rowan_bradley 3 659 Aug-06-2023, 10:58 AM
Last Post: rowan_bradley
  Invalid argument: 'images\x08ackground.jpg' CatBall 4 905 Jun-19-2023, 09:28 AM
Last Post: CatBall
  Couldn't install a go-game called dlgo Nomamesse 14 2,977 Jan-05-2023, 06:38 PM
Last Post: Nomamesse
Information How to take url in telegram bot user input and put it as an argument in a function? askfriends 0 1,033 Dec-25-2022, 03:00 PM
Last Post: askfriends

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020