-1

The variable or the types and the costs:

pasta_types = "Lasagne, Spaghetti, Macaroni, Cannelloni, Ravioli, Penne, Tortellini, Linguine, Farfalle, Fusilli"
pasta_costs = "6.00, 4.00, 3.15, 8.50, 9.00, 3.15, 5.00, 4.00, 4.25, 4.75"

The function to see if the in put has a type of pasta that is in the varaible:

def inventory(pt):
    return(pt.title() in pasta_types)

Input:

type = input('What pasta would you like: Lasagne, Spaghetti, Macaroni, Cannelloni, Ravioli, Penne, Tortellini, Linguine, Farfalle, and Fusilli  ')

Calling Function:

have = inventory(type)

How do I find out what number of the argument that was chosen?

martineau
  • 119,623
  • 25
  • 170
  • 301
Parth. J
  • 11
  • 1
  • 3
  • 2
    Don't use `type` as a variable. It is a keyword in python. Also think about storing your `pasta_types` and `pasta_costs` in `list` instead of a string. You can simply issue `pasta_types = pasta_types.split(',')` and the same for `pasta_costs` to obtain what you're looking for. Keep in mind though that the `costs` are still strings and not `float`. – hqkhan Feb 06 '19 at 17:36
  • 1
    To add to what @hqkhan said, when you call `inventory(type)`, it will call `inventory` on the entire string that was entered in your input, whatever it is. So it won't "choose" anything, because you haven't created a way for it to do so – G. Anderson Feb 06 '19 at 17:39
  • Agreed with @G.Anderson. Also, not sure what `pt.title()` is. There's probably a piece of code that you haven't shown us yet. Should post your entire attempt so you can get a complete answer. – hqkhan Feb 06 '19 at 17:43
  • I think you would find the answers to the question [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) helpful. – martineau Feb 06 '19 at 17:46
  • @hqkhan: Technically `type` isn't a keyword, but it is a built-in `class`, so that's the reason not to redefine it. – martineau Feb 06 '19 at 17:53
  • 1
    @martineau You're right, it isn't. That was my mistake but replacing the `type` function is not good practice. – hqkhan Feb 06 '19 at 17:55

1 Answers1

0

Here's an example of doing something like that which builds and uses a dictionary that associates the name of each pasta type with its cost:

pasta_types = "Lasagne, Spaghetti, Macaroni, Cannelloni, Ravioli, Penne, Tortellini, " \
              "Linguine, Farfalle, Fusilli"
pasta_costs = "6.00, 4.00, 3.15, 8.50, 9.00, 3.15, 5.00, 4.00, 4.25, 4.75"

# Create a dictionary associating (lowercase) pasta name to its (float) cost.
inventory = dict(zip(pasta_types.lower().replace(' ', '').split(','),
                     map(float, pasta_costs.replace(' ', '').split(','))))

## Display contents of dictionary created.
#from pprint import pprint
#print('inventory dictionary:')
#pprint(inventory)

while True:
    pasta_type = input('What pasta would you like: ' + pasta_types + '?: ')
    if pasta_type.lower() in inventory:
        break  # Got a valid response.
    else:
        print('Sorry, no {!r} in inventory, please try again\n'.format(pasta_type))
        continue  # Restart loop.

print('{} costs {:.2f}'.format(pasta_type, inventory[pasta_type]))
martineau
  • 119,623
  • 25
  • 170
  • 301