How to use an if statement in a loop

Question:

I have a simple example. I pass name into serv and loop over a dictionary.
So it should print out in the loop when I give serv a name, but I don’t know why the else is printing

My code:

service = {
        "item1": 22,
        "item2": 31,
    }
def serv(**name):
 selctser = input("pls select your services: ")    

 for ss, sprice in name.items():
     if selctser in ss:
      print (f"Your Services is: {selctser}, and Price is => {sprice}")
     else:
        print('Select good value')
 

allwoedname = ["n1", "n2"]
name = input("Please enter ur name: ")

if name in allwoedname :
    print("Welcone to store booking")
    

else: 
    print("You dont have a login info")


serv(**service)
Asked By: Mahmoud ahmed

||

Answers:

I refactored your code a bit to improve readability:

service = {
    "item1": 22,
    "item2": 31}


def serv(services: dict):
    selected_service = input("PLease select your services: ")

    for ss, price in services.items():
        if selected_service in ss:
            print(f"Your Services is: {selected_service}, and Price is => {price}")
        else:
            print('Select good value')


allowed_names = ["n1", "n2"]
name = input("Please enter ur name: ")

if name in allowed_names:
    print("Welcone to store booking")
else:
    print("You dont have a login info")

serv(service)

Let’s try to pass "n1" as name and "item2" as service. Console will return this:

Please enter ur name: n1
Welcone to store booking
PLease select your services: item2
Select good value
Your Services is: item2, and Price is => 31

Let’s change service dict:

service = {
    "item1": 22,
    "item2": 31,
    "item3": 11,
    "item4": 28}

Now I gonna give wrong name and item3, that’s what console prints:

Please enter ur name: Some name
You dont have a login info
PLease select your services: item3
Select good value
Select good value
Your Services is: item3, and Price is => 11
Select good value

As you can see, if condition for name works properly. But condition for service now prints 4 times – 3 times condition not fulfilled and one for true.

It’s because you iterate over dict with services and check if statment with each iteration. If I understood your intentions right, you should do something like this instead:

def serv(services: dict):
    selected_service = input("PLease select your services: ")

    if selected_service in services.keys():
        print(f"Your Services is: {selected_service}, and Price is => {services[selected_service]}")
    else:
        print('Select good value')

Output:

Please enter ur name: n2
Welcone to store booking
PLease select your services: item2
Your Services is: item2, and Price is => 31

EDIT: I forgot to mention – there is no need to use ** before arguments, since you know how many arguments will be there.

Answered By: LesniakM

its work without loop, and actually i confuse about why code didnot work with loop,
So i loop here into Key and Value

 for ss, sprice in name.items():

And ask if the input is in ss <- Key

  if selctser in ss:

So print the input and price from Value

 print (f"Your Services is: {selctser}, and Price is => {sprice}")

and this the else

else: print('Select good value')

Why i get the error or else printed in all way!

Answered By: Mahmoud ahmed
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.