Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can't get my for loop to produce desired result

Also tried an enumerate and a list comprehension. But I’ll just focus on one thing and comment out the other methods I tried. I think maybe I was supposed to do something involving dictionaries?? But I have no idea how to do that. It wouldn’t even let me define an empty dictionary. So the strategy I decided on is trying to write a program where if the user chooses a name, and that name is found in the list, the item in the index one to the right (which should be the appropriate phone number) will be returned. My for loop for this is towards the bottom.

A contact list is a place where you can store a specific contact with
other associated information such as a phone number, email address,
birthday, etc. Write a program that first takes in word pairs that
consist of a name and a phone number (both strings). That list is
followed by a name, and your program should output the phone number
associated with that name.

Ex: If the input is:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Joe 123-5432 Linda 983-4123 Frank 867-5309
Frank

the output is:

867-5309

 ''' Type your code here. '''
    
    userEntered = input()
    selectedName = input()
    
    
    makeDict = {}
    
    
    
    listLength = len(userEntered.split())
    
    #for key, value in enumerate(makeDict):
        #key = userEntered.split()[0:listLength:2]
        #value =  userEntered.split()[1:listLength:2]
        
        
    #findNumber = [str(x) for x in userEntered.split() if str(x) == selectedName]
    
    #for index, value in enumerate(userEntered.split()):
        #if selectedName == value:
            #print(value[index+1])
        
    
  i = 0
for x in userEntered.split():
    while i <= listLength:
        if x == selectedName:
            answer = x[i+1]
            print(answer)
            break
        i+=1

Enter program input (optional)

Joe 123-5432 Linda 983-4123 Frank 867-5309
Linda

Program output displayed here

(Your program produced no output)

EDIT: Solution now working with:

i = 0
for x in userEntered.split():
    if userEntered.split()[i] == selectedName:
        answer = userEntered.split()[i+1]
        print(answer)
        break
    i+=1

>Solution :

Let me explain you what is not working in your program.

One problem is you inner while loop. You use i there but you don’t use i in condition, so it will always stop at first iteration or at last one. Even if it would work, you don’t reset your i between loops, so it stays the same after while, which probably is not what you ment. If you remove while loop completely it should work!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading