jeudi 22 mars 2018

Python IF with FOR combination

I want to practice thus I made a simple program that goes through the list and prints every word from specific word.

User inputs a number which will indicate position of the word in the list. If the value is out of range error message should be returned which is not happening now.

#user picks number
nb = int(input('Enter value: '))

#available list, code picks the name based on user's input - nb
fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana']
word = fruits[nb][:]

#checks if there is a word on nb position to print it or return error
# 7 should be replaced by range(len(fruits)) but it's not working
if nb >= 7:
    print('out of range')
else:
    for l in word:
        print(l)

Aucun commentaire:

Enregistrer un commentaire