Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 problem in loop
#1
Hey guys..
I wanted to have a loop from list of tuples where i can extract name of husband by giving name of wife..
I got that ... but when I am trying to put one more statement so that if name is not available, it can give precautionary statement, I am having problems.. Please see the code below and suggest what I can do..
PS I do not want to convert it into dictionary.

husbands=[('ginny','harry'),('eve','adam'),('lizzy','darcy'),('rose','jack')]
wife=input('write wife name here = ')
for x in husbands:
    if wife == x[0]:
        loved = x[1]
        print('her husband is ',loved)
    elif wife != x[:]:
        print('name is not available')

Quote
#2
You can try this
husbands=[('ginny','harry'),('eve','adam'),('lizzy','darcy'),('rose','jack')]
wife=input('write wife name here = ')
for x in husbands:
    if wife == x[0]:
        loved = x[1]
        print('her husband is ',loved)
        break
    elif wife != x[:]:
        continue
else: print("Name is not available")
newbieAuggie2019 likes this post
Quote
#3
I would do it this way:
husbands = [('ginny', 'harry'), ('eve', 'adam'), ('lizzy', 'darcy'), ('rose', 'jack')]
name = input('write wife name here = ')
found = False
for wife, husband in husbands:
    if name == wife:
        print(f'{wife}´s husband is {husband}')
        found = True
if not found:
    print(f'{name} might be single as she is not in the list')
Quote
#4
ThomasL refactored code to take advantage of else: clause in for-loop (no need for sentinel):

>>> name = 'lizzy'
>>> for wife, husband in husbands:
...     if  wife == name:
...         print(f"{wife.title()}'s husband is {husband.title()}")
...         break                      
... else:                              # no-break
...     print(f"{name.title()} might be single as she is not in the list")
... 
Lizzy's husband is Darcy
There is of course question of uniqueness. This code returns first match but if there are several wives with same name what is expected result?

For built-in help on for-loop (including else clause): >>> help('for')

EDIT:

One can approach this problem with another angle: get all matching pairs in list and use conditional to print appropriate information:

>>> matches = [f"{wife.title()}'s husband is {husband.title()}" for wife, husband in husbands if wife == name]
>>> if matches:
...     print(*matches, sep='\n')
... else:
...     print(f"{name.title()} might be single as she is not in the list")
...
Lizzy's husband is Darcy
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Dataframe mean calculation problem: do we have to loop? sparkt 1 270 Aug-28-2020, 02:41 PM
Last Post: sparkt
  Python loop problem Kristenl2784 11 669 Jun-18-2020, 07:22 PM
Last Post: buran
  Problem with append list in loop michaelko03 0 345 Feb-16-2020, 07:04 PM
Last Post: michaelko03
  problem with for loop using integers python_germ 5 581 Aug-31-2019, 11:42 AM
Last Post: jefsummers
  Nested while loop problem + turtle DreamingInsanity 3 757 Jul-06-2019, 02:01 PM
Last Post: DreamingInsanity
  Problem Passing Arguement to do loop stephenmolnar 10 1,273 May-13-2019, 02:56 PM
Last Post: Gribouillis
  Nested for loop strange problem mcva 2 676 Mar-16-2019, 12:53 PM
Last Post: mcva
  Problem with a while loop Nassib 2 696 Sep-25-2018, 11:16 PM
Last Post: ichabod801
  Nested Loop Problem GSWes 5 1,843 Nov-12-2017, 10:22 PM
Last Post: Larz60+
  Problem in Loop saumya 2 1,382 Feb-11-2017, 11:30 AM
Last Post: saumya

Forum Jump:


Users browsing this thread: 1 Guest(s)