0

I am trying to a make a simple game. I will supply of a dictionary of states and capitals and a list of states. Using loops and conditionals, I will ask the user if they want to learn some capitals and supply the list of states. The state gets removed from the list and the user should be prompted again if they want to play, repeatedly until the list is empty. With my code right now the loop piece works, if keeps asking if they want to play and as long as the user keeps saying yes my code works running till the list is empty and the loop. But when I try to add a layer for if the player says no and break the loop its not doing anything. Thanks in advance for help!!

states3 = ["NH", "MA", "MS", "SC", "HI"]
print("Let's test my geography skills!")
def state3(states3):
    state_caps = {"NH": "Concord", "HI": "Honolulu", "SC":"Columbia", "MS": "Jackson", "MA":"Boston"}
    play = input("Would you like to learn some capitals:") 
    while play == "Yes" or "yes":
        if play == "Yes" or "yes":
            print ("The states I know the captials of are:", states3)
            yourstate = input("What state do you want to know the capital of: ")
            print("The capital of", yourstate, "is", state_caps.get(yourstate, "That is not a vaild choice"), "!")
            states3.remove(yourstate)
            play = input("Would you like to learn some capitals:") 
        if len(states3) == 0:
            print ("That's it! That's the end of geography skills")
            break

state3(states3)
Rachel Cyr
  • 429
  • 1
  • 5
  • 15
  • 2
    `play == "Yes" or "yes"` doesn't do what you think it does: https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-against-a-value, https://stackoverflow.com/questions/43808947/print-dictionary-minus-two-elements/43808992#43808992 – Nick is tired Nov 20 '20 at 22:52
  • So it isn't matching the value for "play" with the value? I know that yes wouldn't be the same as putting true. I originally had "while L > 0: " and I defined the length of L as the length of the list states before the loop but when I used that that method then it wouldn't acce[t my length argument. – Rachel Cyr Nov 20 '20 at 23:01
  • 1
    It's running `play == "Yes"` which will evaluate to either true or false, and then that true or false is or'd with `"yes" ` (`True or "yes"` for example), this will always evaluate to true – Nick is tired Nov 20 '20 at 23:02
  • Does this answer your question? [How to test multiple variables against a value?](https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-against-a-value) – Pranav Hosangadi Nov 20 '20 at 23:22

2 Answers2

0
while play == "Yes" or play == "yes":
    if play == "Yes" or play == "yes":
        print ("The states I know the captials of..")
        .....
        ....
    elif play == "no" or "No":
        break
DarknessPlusPlus
  • 543
  • 1
  • 5
  • 18
0

Checking for yes/no is problematic. For example, "YES" is not covered in the above code, yet seems a reasonable response. To distinguish between the two choices, do we need to look at the entire word or only the first letter? (string "slicing") Such reduces the set of applicable responses to the set: y, Y, n, N. Now, if we apply the string.lower() or string.upper() method*, we are down to two choices. However, consider this even more closely. Is there really only one user-response that interests us? That the game should continue. Thus, any response other than 'yes' could be considered to mean stop looping!

Another question: once the while-condition has been satisfied and the loop starts to run, is it possible for the first if-condition to be anything other than True? Could the latter be removed then?

Instead of merely adding a "layer", let's review the entire game. How's this for a spec[ification]: repeatedly invite the user to choose a state, and then present its capital. How do we define "repeatedly", or more precisely, how do we end the looping? There are two answers to that question: either all the states have been covered, and/or the user loses interest (the new "layer"). What happens if we use (both of) these to control the while-loop? (NB reversing the logical-statement) Loop if there are states to review and the user consents. Thus, can the last if-condition move 'up' into the while-condition...

Because you are evidently (enjoying) teaching yourself Python, I have left the writing of actual code to your learning experience, but herewith a few items which may increase your satisfaction:-

  • in the same way that the 'heading print()' is outside the loop, consider where the farewell should be located...
  • A list with contents is considered True, whereas an empty/emptied list is considered False. Very handy! Thus, you can ask if states3 (ie without the len()).
  • also consider the upper()/lower() 'trick' when accepting the state's abbreviation-input.
d-n
  • 26
  • 3
  • Hi!!! Thank you for this! I really appreciate your thorough explanation!! These are very helpful. I am actually in a masters program and am in a dedicated python class. However, the lectures provide minimal information and when asking questions we are generally directed to the docs.python website. I rely HEAVILY on kind people like yourself, here on stack, and on the r/learnpython reddit thread (which I know isn't its proper use). So again thank you for this :)! – Rachel Cyr Nov 22 '20 at 23:45
  • 1
    Am pleased to see you referred to THE authority! (kudos to your lecturer). Agreed, Reddit isn't really designed for such, in the same manner as SO (msgs tagged "Python"). Another alternative is to look under "Community" (rather than Docs) on the Python.org web-site, and follow to the Discussion Lists. There is one called "Tutor" where you will receive a non-judgmental reception and find people at exactly your Python-level. (hope I'm allowed to say this without appearing 'disloyal' to SO!) Whilst looking at "Docs", time spent looking at the Tutorial will be a worthwhile investment! – d-n Nov 24 '20 at 05:03