0

I am new to programming, and I am trying to trying to solve this for my assignment(instructions below), but I have few quick questions:

  1. is there any way to simply or condense some of these lines, i feel like I am rendundantly printing when I dont know if there is another way I can have it all in one print line, for instance:
print ("which state capitals do you want to know: ")
print (list(state_dictionary.keys()))

is there a way that can be one print statement?

def state_capitals():
    state_dictionary= {"New Hampshire":"Concord", "Massachusetts": "Boston", "Maine": "Albany", 
                         "Vermont": "M", "New York": "Albany"}
       i=0
       while i <= (len(state_dictionary)):
           print ("which state capitals do you want to know: ")
           print (list(state_dictionary.keys()))

           getcapital= input("enter state: ")
           print(state_dictionary[getcapital])

           state_dictionary.pop(getcapital)

           more= (input("Do you want to know the capital of any more states? Please enter Yes/No: "))

           if more == "yes":
               if bool(state_dictionary):
                   continue

               elif not bool (state_dictionary):
                   print ("i am out of states")
                   break

           elif more == "no":
               break


   state_capitals()
pixe anzu
  • 35
  • 5
  • Does this answer your question? [How to print out n (3) items from a list at a time and then display them on one line. Python 3](https://stackoverflow.com/questions/32295763/how-to-print-out-n-3-items-from-a-list-at-a-time-and-then-display-them-on-one) – Sesquipedalism Mar 03 '20 at 19:13

1 Answers1

1

As far as the print statements go, you can check out f-strings:

somedict = {'a': 1, 'b': 2}

print(f"My dictionary is {somedict}")
My dictionary is {'a': 1, 'b': 2}

or str.format:

print("My dictionary is {}".format(somedict))

The call to list(state_dictionary.keys()) is redundant, list will just consume the keys, so it should be list(state_dictionary)

You don't need bool to wrap your state_dictionary, as dict has a built in __bool__ implementation that is invoked when you call if state_dictionary.

dict.pop on a missing key will raise a KeyError, so catch it in case someone gives you an input you don't expect:

try:
    state_dictionary.pop(getcapital)
except KeyError as e:
    print("Sorry, I don't have that state! Try again")
    continue

You also don't need the if i <= len(dict) for your while loop. You could do a while state_dictionary statement, but you check that condition in the loop, so I'd just do a while True and let your contained conditional logic handle the rest.

I'd change the elif more == 'no' to else, if the user doesn't give you valid input, then you can just end the execution. Since you prompt the user with Yes and No, rather than yes and no, I'd sanitize the input like:

more = more.lower().strip()

This will lowercase everything and get rid of extra newlines/spaces that might be accidentally entered

C.Nivs
  • 12,353
  • 2
  • 19
  • 44