简体   繁体   中英

List comprehension/list creating loop in python 3.x

I've been trying to make a loop or list comprension thingy that does the following:

prints "Please give me the number of bread sold at ", self.bakeryname[k], ":" then prompts the user for the number of break sold at the given bakery, and stores it in a list

"Please give me the number of [breadtype] sold at:"
"The hungry baron": [here you enter a number]
"The flying bulgarian": [here you enter another number]

It's to be stored in a list of integers that starts at the first prompted value and ends at the last -||-.

The number of bakeries is potentially infinite, there's only 3 different types of bread.

I've dug myself into the hole that is this function:

def soldbread(self):
    amount = ((len(self.bakeryname))*3)
    k = 0
    j = 0
    h = 0
    i = 0
    while j < (len(self.breadtype)):
            print("Please give me the number of",self.breadtype[k], "sold at:")
            while i < amount:
                self.breadsold.append(i)
                self.breadsold[i] = int(input(self.bakeryname[h]))
                j += 1
                h += 1
                i += 1
                if k == 3:
                    break
                else:
                    while j >= len(self.bakeryname):
                        k += 1
                        print("Please give me the number of",self.breadtype[k], "soldat:")
                        j = 0
                        h = 0

The function will go to the 15'th type of bread (there's 5 bakeries in self.bakeryname >at the moment<, so atleast that number is accurate) then it will complain about "IndexError: list index out of range". I've tried a bunch of "if"'s and "breaks"'s but I can't pull it off.

The names etc. in the code is translated from my native language, so eventual typos are likely to not be in the code.

bakeries = ['a','b','c']
breadtypes = ['flat','rye','white']

results = []
for i in bakeries:
   print('How many of each type of bread for {0}:'.format(i))
   number_of_types = []
   for bread in breadtypes:
       number_of_types.append(input('{0}:'.format(bread)))
   results.append(number_of_types)

for k,v in enumerate(bakeries):
   print('Here are the different types of breads for {0}'.format(v))
   print(''.join('{0}:{1}'.format(a,b) for a,b in zip(breadtypes, results[k])))
# Your data
bakeries = ['a','b','c']
breadtypes = ['flat','rye','white']
# Output data
results = {}

for bakery in bakeries:
    # A line for each bakery
    print('How many of each type of bread for %s: ' % bakery)

    # The python3 dict comprehension and a generator
    results[bakery] = {
        breadtype: breadtypevalue for breadtype, breadtypevalue
        in ((bt, input('%s: ' % bt)) for bt in breadtypes)
    }

# Wanna print that?
for bakery in results:
    print('Here are the different types of breads for %s:' % bakery)
    print(', '.join(['%s: %s' % (breadtype, number) for breadtype, number in results[bakery].items()]))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM