0

I am trying to iterate through a dictionary and get user response.

At the second to last question, I want to specifically declare that there is one more question left.

I am having trouble combining a while statement with for loop. This is my code:

# my dictionary

q_dict = {"where are we? " : "usa",
          "who are you? " : "jon",
          "how old are you? " : 22}

# my function 

def q_prompt(question, answer):

    response = input(question)

    if response != answer:

        print("incorrect")

    else:

        print("correct!")


# having trouble here with this logic combining for loop and while     

run = 1

for key, val in q_dict.items():
    
    q_prompt(key, val)
    

    while run < (len(q_dict)-1):
        print("on to the next question")
        run += 1

    else:
        print("on to the last question")  # i would like this to print when there is one more question left  

    print("no more questions")



after the user responds to "who are you", I want the program to say "on to the last question". then when the program is done (after user answers the last question", it says "no more questions". I am having trouble iterating through each question and increasing the value of 'run'. Running this code does not get the desired result.

3
  • 1
    What is your question? What happens when you run this code? What do you want it to do? Please edit your question to include more details and to ask a specific question for something you are stuck on. Commented May 16, 2022 at 0:51
  • In your own words, what is the purpose of a while loop? Where the code says print("on to the next question") and then run += 1, how many times should that happen before anything else happens? In your own words, where the code says else:, what do you expect that to line up with, and how do you intend for it to work? In what situation would you expect that code to run? (Hint: while this is not a syntax error, it's not very common. What is the usual thing paired with else?) Commented May 16, 2022 at 0:57
  • "Running this code does not get the desired result." It is not helpful to tell us this. Instead, please explain: what result do you get? How is that result different from the result you want? What specific things did you try in order to understand and diagnose the problem? After doing those things, what is your understanding of the problem? Commented May 16, 2022 at 1:00

1 Answer 1

1

I'm not sure why you'd want to use a while loop here. Why not just have an if-statement like so:

q_dict = {"where are we? " : "usa",
          "who are you? " : "jon",
          "how old are you? " : 22}

for key, val in q_dict.items():
    
    q_prompt(key, val)
    

    if key != list(q_dict.keys())[-2]:
        print("on to the next question")

    else:
        print("on to the last question")  # i would like this to print when there is one more question left  

print("no more questions")

Note, I've also un-indented your final print statement since otherwise it will execute on every iteration.

Sign up to request clarification or add additional context in comments.

4 Comments

I was trying to have a solution that could could be applied to a much larger dictionary with many different questions, not just this simple dictionary. So I want that when the user answers the second to last question, the program says "on to the last question". I hope that is more specific.
See updated answer. Still no need for a while loop. You can simply check against the second-to-last key in your dictionary. @E50M
I was hung up on needing to use a while loop - this solves it quite nicely
@E50M, it's best not to use while loops unnecessarily and they also come with significant performance drawbacks due to how they're implemented in Python.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.