n = int(input())
x = 0
operations = ["++", "--"]
lst = []
for i in range(0, n):
statements = input().split()
lst.append(statements)
for statement in lst:
if operations[0] in lst:
x += 1
if operations[1] in lst:
x -= 1
print(x)
How do I make it so if the users input contains "++" it adds 1 to x and if it contains "--" it will subtract 1 from x?
if operations[0] in lst:
if operations[1] in lst:
都将返回 True,因此您在每个循环中加 1 和减 1。
Your second for loop isn't doing what you're looking for. You are checking to see if '++'
is in the lst
and checking the same for '--'
.
You are already looping over the statement
in lst
, so there is no reason to do that check. Instead you can do:
for statement in lst:
if statement == operations[0]:
x += 1
elif statement == operations[1]:
x -= 1
For the if statements do:
if statement == operations[0]:
x += 1
#And so on...
Your if statements are checking if there is a ++
or --
in the whole list, not each statement.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.