Input for python 3.6.1


#1

When I run the below code on python 3.6.1, I am getting an error:

print ("Exotic fruits") # Name of the program
print ("___________________________")
myfruits = open("fruits.txt", "r") # Opens fruits text file
flist = myfruits.readlines() # Reads each line

for line in flist: # Use for statement to print each line

print (line)                    # Prints each line

myfruits.close() # Closes the file

select = str(input("Select your favourite fruit from the above list: ")) # asks the user to select a fruit
namef = open("fruitinfor.txt", "r") # opens the text file called fruitsinfor.txt
fruitname = namef.readlines() # reads each line of text in the fruitsinfor.txt
if select == kiwi: # if kiwi is chosen by the user it will print information Kiwi fruit.
print(fruitname[0])
print(fruitname[1])
elif select == pitaya: # if Pitaya is chosen by the user it will print information Pitaya fruit.
print(fruitname[2])
print(fruitname[3])
elif select == avocado: # if Avocado is chosen by the user it will print information Avocado fruit.
print(fruitname[4])
print(fruitname[5])
elif select == pineapple: # if Cacao is chosen by the user it will print information Cacao fruit.
print(fruitname[6])
print(fruitname[7])
elif select == papaya: # if Papaya is chosen by the user it will print information Papaya fruit.
print(fruitname[8])
print(fruitname[9])
else: # if entered fruit is not on the list it will display fruit not listed.
print("You have entered a fruit that is not listed")


The error:
Traceback (most recent call last):
line 15, in
if select == kiwi: # if kiwi is chosen by the user it will print information Kiwi fruit.
NameError: name 'kiwi' is not defined

.....

Please help me


#2

why is kiwi a variable, why not a string?


#3

Perhaps try moving the close() to the end of the program. According to the Python documentation:

After calling f.close(), attempts to use the file object will automatically fail.
https://docs.python.org/3.6/tutorial/inputoutput.html


#4

I do not understand what you mean.
By coding it like below is that string or a variable?


select = input("Select your favourite fruit from the above list: ")
namef = open("fruitinfor.txt", "r")
fruitname = namef.readlines()
if select == kiwi:
print(fruitname[0])
print(fruitname[1])


#5

I removed close() to the end of the program.

That did not work
Thanks


#6

here:

if select == kiwi:

currently, kiwi is a undefined variable. Why is it is a variable? Doesn't a string make more sense?


#7

It is not a variable because all I want the program to do is, when user entered kiwi, then it should print line 0 and 1 from the text file.


#8

When I change the input code to integer the program prints line 0 and 1 from the text file.

select = int(input("Select your favourite fruit from the above list: "))
namef = open("fruitinfor.txt", "r")
fruitname = namef.readlines()
if select == 1:
print(fruitname[0])
print(fruitname[1])


#9

But it does not work when I asked the user to type text instead of numbers. I am using python version 3.6.1 from Mac


#10

but on this line:

if select == kiwi:

kiwi is a variable, and as i am trying to tell you, it shouldn't be a variable, it should be string


#11

How do i change it to a sting?

I tired the below code but it did not work:

if select == "kiwi":
print(fruitname[0])
print(fruitname[1])


#12

Thank you very much it worked


#13

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.