-1

Running this code where I am asking the user if they want the code in reverse order.

Everything runs smoothly, however when the program runs the section Do you want reverse order (y/n), it prints None on the next line for some reason?

Can anybody explain why/how I get this to stop?

def farmList():
    print("Please enter six farming animals: ")
    terms = []
    for counter in range(6):
        term = input("Please enter a farm animal")
        terms.append(term)


    reverseOrder = input("Do you want reverse order (y/n)")
    if reverseOrder == "y":
        print(terms[::-1])
    else:
        print(terms)

    whichTerm = int(input("Choose a number between 1-6, and the program will print that animal: "))

    print(terms[whichTerm-1])
vsync
  • 118,978
  • 58
  • 307
  • 400

2 Answers2

1

If you are calling farmList() properly, then changing input to raw_input should work for python 2.7. For Python 2.7, raw_input() takes exactly what the user typed and passes it back as a string. Else for Python 3, your code should work just fine.

Tonechas
  • 13,398
  • 16
  • 46
  • 80
rishon1313
  • 98
  • 8
0

Your code works fine, at least in Python3 (see this thread for more information : What's the difference between raw_input() and input() in python3.x)

Note that you only show a reversed list, you don't reverse terms, though :

Please enter six farming animals: 
Please enter a farm animala
Please enter a farm animalb
Please enter a farm animalc
Please enter a farm animald
Please enter a farm animale
Please enter a farm animalf
Do you want reverse order (y/n)y
['f', 'e', 'd', 'c', 'b', 'a']
Choose a number between 1-6, and the program will print that animal: 1
a

To reverse the list you could write :

if reverseOrder == "y":
    terms = terms[::-1]

So your code would become :

def farmList():
    print("Please enter six farming animals: ")
    terms = []
    for counter in range(6):
        term = input("Please enter a farm animal")
        terms.append(term)


    reverseOrder = input("Do you want reverse order (y/n)")
    if reverseOrder == "y":
        terms = terms[::-1]

    whichTerm = int(input("Choose a number between 1-6, and the program will print that animal: "))

    print(terms[whichTerm-1])

farmList()

For Python2.7, replace every occurence of input() to raw_input().

Community
  • 1
  • 1
Eric Duminil
  • 52,989
  • 9
  • 71
  • 124