如何将继续添加到if块

时间:2017-08-04 00:12:29

标签: python if-statement

我如何向 Name Price 0 HP - 15.6" Laptop - AMD A6-Series - 4GB Memory... $241.99 1 HP - 15.6" Laptop - Intel Core i5 - 8GB Memory... $399.99 2 Lenovo - Ideapad 110s 11.6" Laptop - Intel Cel... $169.99 3 Dell - Inspiron 15.6" Touch-Screen Laptop - In... $349.99 4 HP - 15.6" Laptop - Intel Core i5 - 8GB Memory... $449.99 5 HP - 17.3" Laptop - Intel Core i7 - 8GB Memory... $529.99 6 Dell - Inspiron 11.6" Laptop - Intel Celeron -... $174.99 7 Lenovo - 15.6" Laptop - AMD A6-Series - 4GB Me... $229.99 8 Dell - Inspiron 15.6" Touch-Screen Laptop - In... $349.99 9 Lenovo - Flex 4 14 2-in-1 14" Touch-Screen Lap... $349.99 10 Dell - Inspiron 17.3" Laptop - AMD A9-Series -... $506.99 11 Dell - Inspiron 17.3" Laptop - Intel Core i7 -... $938.99 12 Samsung - 11.6" Chromebook - Intel Celeron - 4... NaN 13 Acer - 15.6" Chromebook - Intel Celeron - 4GB ... $219.99 14 Lenovo - Yoga 710 2-in-1 11.6" Touch-Screen La... $399.99 15 Dell - Inspiron 2-in-1 17.3" Touch-Screen Lapt... $849.99 16 HP - Spectre x360 2-in-1 13.3" Touch-Screen La... $1,279.99 17 Asus - 2-in-1 13.3" Touch-Screen Laptop - Inte... $1,129.99 18 HP - 15.6" Laptop - AMD A12-Series - 6GB Memor... $299.99 19 Asus - ROG GL502VM 15.6" Laptop - Intel Core i... $1,149.99 20 Lenovo - 15.6" Laptop - Intel Core i3 - 6GB Me... $312.99 21 Lenovo - Flex 4 1130 2-in-1 11.6" Touch-Screen... $229.99 22 Samsung - 12.3" Chromebook Plus - Touch Screen... $399.00 23 HP - Spectre x360 2-in-1 13.3" Touch-Screen La... $1,129.99 添加一个继续我不理解if语句,我需要添加一个继续作为class_ = eqid if eqid in allowed_classes else default_class然后我希望while循环停止,我也如果if eqid == allowed_classes

,则只希望它打印所选的课程
eqid == allowed_classes

我试过了

allowed_classes = set(["N001", "N002", "N003", "N004", "E001", "E002", "E003"])  # etc
default_class = "N004" #If no class is specified it is defaulted to N004

eqid = raw_input('Please swipe your card: ').strip().upper()
class_ = eqid if eqid in allowed_classes else default_class
print("Selected class", class_)

但这不起作用,甚至不会运行。

完整代码:

eqid = raw_input('Please swipe your card: ').strip().upper()
if eqid in allowed_classes 
    class_ = eqid
    print('Selected class: ', class_)
else default_class

(格式正确无法在此处格式化抱歉:/)

1 个答案:

答案 0 :(得分:2)

简单的答案是你做不到。您将ternary operatorif statement混淆,可能是因为它们有一些语法相似性。三元运算符根据条件返回两个表达式之一:

expr1 if condition else expr2

这是表达式,而不是语句块。您无法在表达式中添加continue

相反,你必须使用完整的表格:

if eqid in allowed_classes:
    class_ = eqid
    print('Selected class: ', class_)
    continue
else:
    class_ = default_class

这就是你需要的吗?