-1

I am trying to have the user type in a suite and if the suite isnt in the dictionary then it prints not in the dictionary, but it the input is a suite then it prints the suite

I have tried everything I know how to do

a = {'hearts':"hearts",'diamonds':"diamonds",'spades':"spades",'clubs':"clubs"}

while b=input("Pick a Suite"):

    if a = b:
        print(b)
    else:
        print(a, "not a suite")

    print("foo not a suite")

again if the user types in right suite then it will print the suite if not it will print "foo not a suite"

BenT
  • 3,172
  • 3
  • 18
  • 38

4 Answers4

0

Try the code:

a = [your_suits]
b = input('enter suite')

if b in a:
    print(b)
else:
    print("foo not a suite")
SdahlSean
  • 573
  • 3
  • 13
  • First of all why are you using a dictionary and not simply a list when key and element are the same? (That's why I changed it to []) – SdahlSean Mar 31 '19 at 04:36
  • Second of all why are you using a while loop there the program pauses as long as no input is entered? (If you want the program to run over and over again just add a while True: around everything) – SdahlSean Mar 31 '19 at 04:37
  • Furthermore you are comparing the dictionary to the input and not elements in it. And also you used = instead of == so actually you are trying to assign a to b – SdahlSean Mar 31 '19 at 04:39
  • A dictionary is much more efficient on terms of lookup than a list. Ideally, a set should be used. – DYZ Mar 31 '19 at 05:31
0

I am not sure why the while loop is necessary for your case, maybe you will build on it later. But you can use the below code. Note there are several bugs in your code if you want it to do what you state in your question.

a = {'hearts':"hearts",'diamonds':"diamonds",'spades':"spades",'clubs':"clubs"}

b=input('Pick a Suite: ') #Use raw_input for string values in Python 2.x

while True: #This is not necessary as stated by SeanSdahl
    if b in a: # Use in to check if value is in the dict, not ==
        print(b)
        break

    else:
        print(b + " is not a suite") # To combine strings use +
BenT
  • 3,172
  • 3
  • 18
  • 38
  • You should not assume Python 2.x unless the question explicitly asks for it. New code in 2019 should definitely target Python 3. – tripleee Mar 31 '19 at 05:32
  • Can you clarify what is different in Python 3 that is different for this code? – BenT Mar 31 '19 at 05:37
  • 1
    There is no `raw_input` in Python 3. The regular `input` has sane semantics, unlike in Python 2. – tripleee Mar 31 '19 at 05:38
  • Don't `break` on invalid input; surely looping back and asking again is the whole point of the `while` loop. – tripleee Mar 31 '19 at 06:54
0
a = {'hearts': "hearts", 'diamonds': "diamonds", 'spades': "spades", 'clubs': "clubs"}

key = input("Pick a Suite, please enter: ")

if key in a:
     print(a[key])
else:
    print('There is no element with key "' + key + '" in dict')
  • Please describe, what did you change and why, to help others identify the problem and understand this answer – FZs Mar 31 '19 at 06:26
0

Here's a refactoring, with inline comments.

# Use a set instead of a (silly) dict
# Use a descriptive variable name
suites = {'hearts', 'clubs', 'diamonds', 'spades'}

# Can't use an assignment in while
while True:
    # Use a descriptive variable name
    suite = input("Pick a Suite: ")

    # Use "in" for checking membership
    # ("=" was doubly wrong; equality comparison is "==")
    if suite in suites:
        break
    # else
    print('{0} is not a suite'.format(suite))

print('Valid suite')
# ... More code here
tripleee
  • 175,061
  • 34
  • 275
  • 318