程序将要求用户提供套件并打印该套件

时间:2019-03-31 04:31:50

标签: python

我正在尝试让用户输入套件,如果套件在词典中不存在,则它不会在词典中打印,但是输入的是套件,则它会打印套件

我已经尽力尝试一切

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”

4 个答案:

答案 0 :(得分:0)

尝试输入代码:

def mostExpensiveStock(self):
    mostexpStock = 0
    for r in self.getstockList():
        if r.value(r.getamountPur, r.getpricePur) > mostexpStock:
            mostexpStock = r.value(r.getamountPur, r.getpricePur)
            highest = str(r) + "with a total value of RM" + str('%.2f'%mostexpStock)
    return highest

答案 1 :(得分:0)

我不确定为什么您的案例需要while循环,也许您稍后会在其上进行构建。但是您可以使用以下代码。请注意,如果您希望代码执行您在问题中陈述的内容,则代码中存在多个错误。

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 +

答案 2 :(得分: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')

答案 3 :(得分:0)

这里是重构,带有内联注释。

>>> import cv2
>>> dir(cv2.face)
['BIF_create', 'EigenFaceRecognizer_create', 'FisherFaceRecognizer_create', 'LBPHFaceRecognizer_create', 'MACE_create', 'MACE_load', 'StandardCollector_create', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'createFacemarkAAM', 'createFacemarkKazemi', 'createFacemarkLBF', 'drawFacemarks', 'getFacesHAAR', 'loadDatasetList', 'loadFacePoints', 'loadTrainingData']