Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I freezed
#11
I hope I understood your requirement right... so here is what I think you should do:
You can loop over keys and values in your dictionary like this:
for key, value in inventory.iteritems():
Inside the loop, you can check if "key" (which is a string) contains substring "juice" with:
if "juice" in key
Reply
#12
thank you bro but it will connected with what ı write ?
for example ı write juice program give only juice items or ı write eggs program must give me only eggs
is it possible ?
Reply
#13
Sure it is possible. But you need to make sure your input matches the keys in your dictionary. If you enter you want "juice", then all keys that contain this substring will be found. If you enter "egg" or "eggs", all keys which contain this substring will be found. If you say you want "pancake" and there is no key containing this substring, nothing will be found. Or if you have a key that contains "pancake" and you input "pancakes", it will not be found either.
Reply
#14
I misunderstood that I could not do it in any way.I try your code but it again give all inventory.

sorry ı waste your time ı'm grateful you..

    if answer=="1":
                        inventory = {'asparagus': [10, 5], 'broccoli': [15, 6], 'carrots': [18, 7],
                         'apples': [20, 5], 'banana': [10, 8], 'berries': [30, 3],
                         'eggs': [50, 2], 'mixed fruit juice': [0, 8], 'fish sticks': [25, 12],
                         'ice cream': [32, 6], 'apple juice': [40, 7], 'orange juice': [30, 8],
                         'grape juice': [10, 9]}
                        message3=raw_input("Please select which item you want to add to your basket (Enter 0 for main menu): ")
                        for message3 in inventory.items():
                            print message3
Reply
#15
Except for raw_input message, what is the change since the last code you posted? I don't see implementation of my suggestion, or any other attempt to change code behaviour.

No need to apologize, we are all in the process of learning, all the time.
Reply
#16
sorry ı guess ı dont understand your code. can you show me with my codes your suggestion.
Reply
#17
for key, value in inventory.iteritems(): # inventory.items() - if you use Python 3
    if message3 in key:
        print value
Reply
#18
ı appreciate you.you must be hero thank you friend.it Works... :)))) <33 if ı take another problem will ı ask you if you want ?
Reply
#19
I am glad it works, but I really hope you understand how it works! If not, feel free to ask.
This forum is full of heroes. So any problem you have, post it here and someone will answer ;)
Reply
#20
how ı add inventory to basket?
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020