A Boima
A Boima

Reputation: 117

Python 3.5 "name 'i' is not defined."

I am an absolute beginner to programming.

The issue I am having is not being able to have the inputs be sent to the modulo line where the "if statement " begins.

My aim is, after the three inputs are entered, only the input that is divisible by 5 should be printed. I get an error: NameError: name 'i' is not defined.

I have not been able to figure out how to fix this so, I am asking for help please. The code is below:

bo=0
xo=0
zo=0
while True:
    bo,xo,zo=[int(i)for i in input('Enter three numbers:  ').split(" ")]
    print([bo,xo,zo])
    if i%5 == 0:
        print([i])
    else:
        print('Sorry')

Upvotes: 0

Views: 5480

Answers (2)

NinjaKitty
NinjaKitty

Reputation: 638

You say in your requirements

My aim is, after the three inputs are entered, only the input that is divisible by 5 should be printed

Yet, you print something when something isn't divisible by 5... The word "sorry"

Anyways here is my answer:

[print(i) for i in input('Enter three numbers: ').split() if int(i) % 5 == 0]

It will print i if it is divisible by 5. Otherwise it won't print anything.

Need it to print as a list?

print([int(i) for i in input('Enter three numbers: ').split() if int(i) % 5 == 0])

Upvotes: 2

prudhvi Indana
prudhvi Indana

Reputation: 829

input('Enter three numbers: ').split() takes space seperated integers and contverts them to a list as ['30','40','2'] map applyes int function to all elements of ['30','40','2'] list converting it to [30,40,2] Now I have my list prepared I just need to iterate over the list and check if its item is divisible by 5 and print number or 'sorry' based on remainder obtained after division.

items = map(int,input('Enter three numbers:  ').split())
for item in items:
  if item%5 == 0:
    print (item)
  else:
    print ('sorry')

output:

Enter three numbers:   30 40 2
30
40
sorry

Upvotes: 0

Related Questions