Beginner in Python

Asked

Viewed 120 times

0

I can’t find a way to check several variables at once. Look what I tried:

Example

    nota1 = 10
    nota2 = 20
    nota3 = 30

for i in (1, 2, 3)
    if nota{i} == 0 
    print('É zero')

In this case, there are many variables so it would be impossible to write one by one. Is there any solution?

  • You need to know if one of the variables is zero?

  • Hi Lucas, could you give more information about what you need to do? I could not understand very well. Explain a little better your problem

  • Do you want to know if the note 0 is inserted? Make a list of the notes and then use the if conditional together with the in.

2 answers

4

Use an array:

notas = [10, 20, 30]
for nota in notas:
    if nota == 0:
        print("É zero")
  • 1

    It wasn’t simpler to do 0 in notas?

  • I don’t think so. I think it’s just a print. The real goal is to do something with the note that is 0, and not just check whether there is one or not

-1

an alternative not very simple, but that works:

notas = ['10','20','30','40','0','0']
sem_zeros = [n for n in notas if n != '0']
for nota in sem_zeros:
    print(int(nota))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.