首页 > 解决方案 > 程序将向用户询问套件并打印该套件

问题描述

我试图让用户输入一个套件,如果该套件不在字典中,那么它不会在字典中打印,但输入是一个套件,然后它会打印该套件

我已经尝试了我知道该怎么做的一切

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")

再次,如果用户键入正确的套件,那么它将打印套件,如果不是,它将打印“foo not a suite”

标签: python

解决方案


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')

推荐阅读