The best way to make a prompt dependend on what's in a dictionary

Been playing around with your advice.
the restaurante is a local map I’ll slot into the overall game engine.

I have made a few upgrades:

import random

class Restaurnante(object):


    restaturantChoice = [
    ['burgerjoint', 'Local five guys'],
    ['cafe','The greasy spoon around the corner'],
    ['vegan','The new indie vegan cafe']
    ]

    def go(self):
        if len(self.restaturantChoice) > 0:
            prompt = self.promptcreation(self.restaturantChoice)
            choice = input('Which do you choose?\n>>> ')

            if str(choice).isdigit() and len(prompt) >= int(choice) > 0:
                date = prompt.pop(int(choice)-1)
                print(f'Very well, you chose the: {date[1]}')
            else:
                date = prompt.pop(random.randint(1,len(prompt)-1))
                print(f"If you don't know how to key in the right number we'll just send you on a date\nYou are going {date[1]}")

            self.restaturantChoice = prompt
            return date[0]

        else:
            print('no more restaureantes to choose from')
            return 'No restaurante'

    def promptcreation(self, state):
        print('These are the restaurants thats not full tonight')
        newprompt = []

        for value, place in enumerate(state,1):
            print(f'{value}) {place[1]}')
            newprompt.append(place)

        return newprompt


returnspot = Restaurnante()
date1 = returnspot.go()
print(date1)

date2 = returnspot.go()
print(date2)

date3 = returnspot.go()
print(date3)

date4 = returnspot.go()
print(date4)

I understand how you can squeeze everything in my promptcreation function down to:

return '\n'.join(f"{key}) {value[1]}" for key, value in enumerate(state,1))

But I still don’t get why I get the error when I do:

f"{key}) {value[1]}" for key, value in enumerate(state,1)

As both variables are mentioned on both side of the ‘for’

1 Like

Looking great!

Food for thought: What if you didn’t need prompt? Could you make the whole thing work just on self.restaurantChoice? What would promptcreation() do in that case?

I’ve got to go now, I’ll try to explain more about this later. For now, just try and see what happens if you put the expression in brackets or parentheses. (Try both.)

1 Like

Thanks for all that help @florian.
It works returning everything as a list with [ ] around it. I guess thats why its a list-comprehension :wink:
You are right if I use a list comprehension then my promptcreation function becomes a little pointless.
So I managed to squeeze it further down to the below:

import random

class Date(object):
    restaturantChoice = [
    ['burgerjoint', 'Local five guys'],
    ['cafe','The greasy spoon around the corner'],
    ['vegan','The new indie vegan cafe']
    ]


    def restaurante(self):

        if len(self.restaturantChoice)>0:
            prompt='\n'.join(f"{key}) {value[1]}" for key, value in enumerate(self.restaturantChoice,1))
            NewRestoList = [place for place in self.restaturantChoice]

            choice = input(f'These are the restaurants thats not full tonight:\n{prompt}\nWhich one do you choose?\n>>>')

            if str(choice).isdigit() and len(NewRestoList) >= int(choice) > 0:
                date = NewRestoList.pop(int(choice)-1)
                print(f'Very well, you chose the: {date[1]}')
            else:
                date = NewRestoList.pop(random.randint(1,len(NewRestoList)-1))
                print(f"If you don't know how to key in the right number we'll just send you on a date\nYou are going {date[1]}")

            self.restaturantChoice = NewRestoList
            return date[0]


returnspot = Date()
date1 = returnspot.restaurante()
print(date1)

date2 = returnspot.restaurante()
print(date2)

date3 = returnspot.restaurante()
print(date3)

Great, I’m glad I could help! :slight_smile:

A few more tips:

  • input() alwalys returns a string, so there’s no need to do str(choice) before you call isdigit().
  • Also, int(choice) kind of does that test for you, though it will raise an error if it fails. It’d be a bit of an overkill here, but at some point I would encourage you to read up on the try statement because it allows you to handle such combined tests very elegantly.
  • Do you know that lists have a list.copy() method?
  • The random choice line is of course fine. It might be slightly more readable if you did date = random.choice(list); list.remove(date). :wink:

And lastly, you’re doing this:

self.restaurantChoice ---- make a copy ----> NewRestoList
                                                  |
                                         change it with pop()
                                                  |
self.restaurantChoice <---- assign it -----  NewRestoList 

Dumb question: Why the detour?

1 Like

Hi @florian

Thanks for all this help. Im get to exercise 45 where I’ll recreate the whole game again but with more bells and whistles and keep all your tips in mind…

Thank you again :pray: