Home > Net >  Create a function utilizing customized numbers of input at the same time
Create a function utilizing customized numbers of input at the same time

Time:02-27

I tried to create a function that accommodates and uses every element in a list at the same time, and the number of elements in a list is the user input. That means it varies from time to time. It can be a list of 3 elements or 4 elements or whatever is the number. It's kinda abstract. I will illustrate it with some code.

Assume that I want to see if the 2 alphabets in list A are in B.

A = ['a', 'b']
B = 'apple'

if A[0] in B and A[1] in B:
    print (f'{A[0]} and {A[B]} are in {B}!')
else:
    print (f'{A[0]} or {A[1]} is not in {B}!')

But if I want to accommodate another situation that the number of alphabets is 3? I can't think of a better way but to make it redundant like this.

A = ['a', 'b', 'c']
B = 'apple'
if len(A) == 2:
    if A[0] in B and A[1] in B:
        print (f'{A[0]} and {A[1]} are in {B}!')
    else:
        print (f'{A[0]} or {A[1]} is not in {B}!')
elif len(B) == 3:
    if A[0] in B and A[1] in B and A[2] in B:
        print (f'{A[0]}, {A[1]}, and {A[2]} are in {B}!')
    else:
        print (f'{A[0]}, {A[1]}, or {A[3]} is not in {B}!')

And actually I am building a solver for the famous game Wordle, so you can foresee that because of the length of words in Wordle, I have the if statement from len(A) == 1 to len(A) == 5. That is pretty lengthy. I know there is a bunch of Wordle solvers on the Internet, but I am just trying to make a project on my own and, as a newbie coder, practice my coding skills. Thanks for helping me out in advance.

CodePudding user response:

b = ['p', 'o']
a = 'pippo'


print('set of list ',b,' is : ',set(b))
print('set of string ',a,' is : ',set(a))

print('          ')

def match(a, b):
    if set(b).issubset(a):
        print('true')
        print(str(b)[1:-1], 'are in ', a,'\n')
        return True
    else:
        print('false')
        print('one of : ',str(b)[1:-1], 'is not in ', a,'\n')
        return False
        
match('pippo', ['p','o','i'])

match('pippo', ['p','q'])



output:

set of list  ['p', 'o']  is :  {'p', 'o'}
set of string  pippo  is :  {'p', 'i', 'o'}
          
true
'p', 'o', 'i' are in  pippo 

false
one of :  'p', 'q' is not in  pippo 



CodePudding user response:

You can try this way

def wordle(word):
    match_alphabet = []
    not_match_alphabet = []
    no_of_alphabet = int(input("How many alphabets?(Max 5)\n", ))
    assert no_of_alphabet <=5, "Maximum number of alphabets is 5"
    
    alphabets = [input("Input your alphabet\n",) for i in range(no_of_alphabet)]
    
    for j in alphabets:
        if j.lower() in word.lower():
            match_alphabet.append(j)
        else:
            not_match_alphabet.append(j)
    
    print(match_alphabet,"are in ", word)
    print(not_match_alphabet,"are not in", word)

word = "Fried"
wordle(word)

And this is the output:

How many alphabets?(Max 5)
5

Input your alphabet
A

Input your alphabet
E

Input your alphabet
I

Input your alphabet
O

Input your alphabet
U
['E', 'I'] are in  Fried
['A', 'O', 'U'] are not in Fried
  • Related