vendredi 6 septembre 2019

Python: Changing specific list elements using If statemand and Loop [duplicate]

This question already has an answer here:

The user would enter the first letter of the fruit which goes into the list, which then are supposed to change into the words of the fruit they chose. but for some reason every element just changes into guava.

#fruits
q = []

#basket
w = []

print("Catch and eat any of these fruits")
q.append('apple')
q.append('orange')
q.append('mango')
q.append('guava')

x = int(input("How many fruits would you like to catch?"))

print("Choose a fruit to catch. Press A, O, M, or G.")

for c in range (1, x+1):

        k = input('Fruit ' + str(c) + ' of ' + str(x) + ': ')

        w.append(k)
        print (w)

for h in range (0, x):
            if w[h] == 'm' or 'M':
                w[h] = 'mango'
            if w[h] == 'o' or 'O':
                w[h] = 'orange'
            if w[h] == 'a' or 'A':
                w[h] = 'apple'
            if w[h] == 'g' or 'G':
                w[h] = 'guava'

print(w)

user input: [A, G, M] expected: [Apple, Guava, Mango]

what I keep getting is:

[guava, guava, guava]

Aucun commentaire:

Enregistrer un commentaire