简体   繁体   English

为什么我的 if...in 语句在 python 中不起作用?

[英]Why doesn't my if...in statement work in python?

this program takes 3 lists of numbers, and compares A and B to the list n.该程序采用 3 个数字列表,并将 A 和 B 与列表 n 进行比较。 if a term from A is in n, the happiness increases.如果 A 中的一项在 n 中,则幸福感增加。 if a term from B is in n, the happiness decreases.如果 B 中的一项在 n 中,则幸福感降低。 However, when I am doing these calculations, the if... in statement to check if a term from A/B is in n doesn't work - I have done print(happy) after each one to check, and I get no result但是,当我进行这些计算时,用于检查 A/B 中的术语是否在 n 中的 if...in 语句不起作用 - 我在每个检查后都完成了 print(happy),但我没有结果

A = []
B = []
n = []
happy = 0
lengthn, lengthAB = input("").split()
for i in lengthn:
    numbers = input("")
newNumbers = numbers.split()
n.append(newNumbers)
for i in lengthAB:
    numbers = input("")
ANumbers = numbers.split()
A.append(ANumbers)
for i in lengthAB:
    numbers = input("")
BNumbers = numbers.split()
B.append(BNumbers)

long = int(lengthAB)
for i in range(long):
    j = int(i)
    if A[j - 1] in n:
        happy = happy + 1
        print(happy)
    if B[j - 1] in n:
        happy = happy - 1
        print(happy)
    i = i + 1

print(happy)

Thank you so much for the help!!非常感谢你的帮助!!

You appended a list to n , not each element of that list.您将一个列表附加到n ,而不是该列表的每个元素。 You can write你可以写

n.extend(newNumbers)

instead.反而。

You could just write n = newNumbers.split() , but as pointed out in a comment, you probably have an indentation error:可以只写n = newNumbers.split() ,但正如评论中指出的那样,你可能有一个缩进错误:

for i in lengthn:
    numbers = input("")
    newNumbers = numbers.split()
    n.extend(newNumbers)

Or, you don't need split at all:或者,您根本不需要拆分:

for i in lengthn:
    number = int(input(""))
    n.append(number)

At some point, you probably mean to convert the string inputs to integers;在某些时候,您可能打算将字符串输入转换为整数; may as well do that immediately after reading the string.也可以在阅读字符串后立即执行此操作。 (I'm declaring various techniques for handling conversion errors beyond the scope of this answer.) (我声明了各种技术来处理此答案的 scope 之外的转换错误。)

Contrary to what you seem to expect the variables: lengthn, lengthAB are strings与您似乎期望的变量相反: lengthn, lengthAB是字符串

The for -loop for循环

for i in lengthn:
    numbers = input("")

iterates over the characters in the string lengthn.遍历字符串 lengthn 中的字符。 If lengthn='12' it will ask to provide input twice.如果lengthn='12'它将要求提供两次输入。

If lengthAB is '13' for example you will get 2 numbers in your list BNumbers but later on you try to test 13 values because int('13') is 13.例如,如果 lengthAB 为“13”,您将在列表 BNumbers 中获得 2 个数字,但稍后您尝试测试 13 个值,因为 int('13') 为 13。

 for i in lengthn:
     numbers = input("")

so the numbers you are getting are the form of string it's will iterate on string rather then a number.所以你得到的数字是字符串的形式,它将在字符串而不是数字上迭代。

You should look for beeter python book.您应该寻找 beeter python 书。 Based on desription I think this should look like this:根据描述,我认为这应该是这样的:

def happiness(A, B, n):
    return sum(x in n for x in A) - sum(x in n for x in B)

def get_data(prompt=""):
    return [int(x) for x in input(prompt).split()]

print(happiness(get_data(), get_data(), get_data()))

https://ideone.com/Q2MZCo https://ideone.com/Q2MZCo

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

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