简体   繁体   English

像开关盒一样更改值?

[英]Changing the values like a switch-case?

I am trying to get the value of "a" like a switch-case.我试图像开关盒一样获得“a”的值。 There are only two values for "a" which is either 0 or 18. Let's take 18 as the intial value, when the length of the name is less than or equal to 5 at first name "Devin", the value is "a" is 18 first, and when the length is greater than 5, the value has to switch to 0 and stay 0 till the next length name greater than 5 doesn't show up. “a”只有两个值,即 0 或 18。让我们以 18 作为初始值,当名字的长度小于或等于 5 的名字“Devin”时,值为“a”首先是 18,当长度大于 5 时,该值必须切换为 0 并保持 0,直到下一个大于 5 的长度名称不出现。 The output should be 18,0,0,0,18,0. output 应为 18,0,0,0,18,0。 The program should flip the values only when the name with length greater than 5 appears.仅当出现长度大于 5 的名称时,程序才应翻转值。

This is what I tried这是我尝试过的

names = ["Devin","Ashish","Rachi","David","Dohyun","Seonhwa"]
for i in range(len(names)):
    #print(len(names[i]))

    if len(names[i])<=5:
        a =18
    else:
        a=0

    print(a)
names = ["Devin", "Ashish", "Rachi", "David", "Dohyun", "Seonhwa"]
# Pick a start value based on the first name
a = 18 if len(names[0]) <= 5 else 0
print(a)

for name in names[1:]:
    if len(name) > 5:
        a = 0 if a == 18 else 18
    print(a)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM