比较Python输入列表中的字符串

铁托·尤达

我有这样的代码:

n = list(input().split()) #Input score in alphabet
for i in n :
    if i == "E" : 
        print("You Have Bad Score") #if you have an E score, then you cant pass
    elif len(n) < 7 :
        print("Your score does not qualify") #must enter at least 8 values
    else :
        print("You pass")

# Output
A B C C D D D E = Your have bad score
A B B A A B B C = You Pass
A B B B A = Your score doesn't qualify

但是我的代码中的问题是,当我输入ABCCDDDE时,输出为“ You Pass”而不是“ You score bad”。你能帮助我吗 ?

Daniel Hao

您的代码有一些错误的类型或错误:1.x应该为n其次,尝试使用更具描述性的变量名来帮助您。[注意-您仍然必须修复/解决多个打印语句问题...]

试试这个应该可以帮助您找到答案。

scores = list(input().split()) #Input score in alphabet
print(scores)    # you can comment this out 

for s in scores:

    if any(s == 'E' for s in scores): 
        print("You Have Bad Score")   # earlier bail-out when `E` scores.
        break
    elif len(scores) < 7 :
        print("Your score does not qualify") #must enter at least 8 values
    else :
        print("You pass")

# Output
# A B C C D D D E = Your have bad score
# A B B A A B B C = You Pass

#A B B B A = Your score doesn't qualify

# Sample Run - with # first list of given scores:
A B C C D D D E
['A', 'B', 'C', 'C', 'D', 'D', 'D', 'E']

You Have Bad Score           # changed as the new Req. asked! 

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在python中将输入列表转换为字符串

如何替换用户输入列表中的某个字符串?

如何通过用户输入将字符串和整数放入列表中

Postgresql jsonb查询,它使用字符串输入列表并比较表的所有记录,并需要返回与字符串匹配的列

如何将csv文件中的特定字符串放入列表中-python 3.4

列表中的python字符串比较问题

比较python中列表和矩阵的字符串

Python将列表中的部分字符串相互比较

Python 3:For 循环比较列表中的字符串

重新加入列表中的特定分割字符串PYTHON

从列表中的字符串比较日期

比较列表中的字符串

将输入(字符串)视为 Python 中的列表

将用户输入与字符串数组列表进行比较

如何将用户输入与列表中的大写字母字符串进行比较?

比较使用C#在列表中是否存在用户输入的字符串

比较字符串和字符串列表以查找Python中的字谜

PHP-将输入列表转换为JSON字符串

将列表列表中字符串的出现与python中的字典进行比较

如何比较python中的字符串

比较python中的日期字符串

比较Python中的字符串索引

Python中的字符串比较?

Python:比较具有两个不同列表长度的列表中的字符串

将字符串插入列表中而不会拆分为字符

比较列表中的字符串和列表中的字符串

如何将用户输入的字符串与列表中单词的字符(单个字母)进行比较?

Python:如何比较 if 语句中的用户输入字符串?

如何比较列表中的元组中的字符串