0

I'm just a newbie to Python so my code probably doesn't look that good, but it works fine except one thing. If for example my listofnumberofpancakes is [1,2,3,3] and I'm looking for the max which is 3, it will print out index 2 and 3. But, later on in my code, when I want to print the values of my listofnames of index 2 and 3, it will print person2 ate the most pancakes and on a new line person3 ate the most pancakes, how can I put that together? Like: person2 and person3 ate the most pancakes. Here's my code:

pancakes
listofnames = []
listofnumberofpancakes = []
print "I will ask you ten times to give in the name of a person and how many pancakes he/she ate."
for count in range(0,10):
    name = str(raw_input("What's the name of the person?"))
    numberofpancakes = int(raw_input("And how many pancakes did he/she eat?"))
    listofnames.append(name)
    listofnumberofpancakes.append(numberofpancakes)
print "\n"
print "The max of pancakes eaten by a person was: ", max(listofnumberofpancakes)
print "The minimum of pancakes eaten by a person was: ", min(listofnumberofpancakes) 
for i, j in enumerate(listofnumberofpancakes):
    if j == max(listofnumberofpancakes):
        print listofnames[i], "ate the most pancakes"
for lala, a in enumerate(listofnumberofpancakes):
    if a == min(listofnumberofpancakes):
        print listofnames[lala], "ate the least pancakes"
print listofnumberofpancakes

Example of an output:

The max of pancakes eaten by a person was:  9
The minimum of pancakes eaten by a person was:  1
emma ate the most pancakes
tina ate the most pancakes
ina ate the least pancakes
[3, 3, 2, 5, 9, 9, 2, 1, 8, 4]
1
  • Alright, thanks for all the help! I understand. :) Commented Oct 11, 2015 at 8:52

3 Answers 3

1

You need to save your print statements for when you're done looping. It also helps to give your variables meaningful names, and to use blank lines to logically separate your code. I removed the interactive portion of the code for faster testing.

listofnames = ['alice', 'bob', 'charlie', 'dan']
listofnumberofpancakes = [1,2,3,3]

top_eaters = []
for i, pcakes in enumerate(listofnumberofpancakes):
    if pcakes == max(listofnumberofpancakes):
        top_eaters += [listofnames[i]]

bottom_feeders = [] # get it?  it ryhmes with... nevermind
for i, pcakes in enumerate(listofnumberofpancakes):
    if pcakes == min(listofnumberofpancakes):
        bottom_feeders += [listofnames[i]]


print("The max of pancakes eaten by a person was: ", max(listofnumberofpancakes))
print("The minimum of pancakes eaten by a person was: ", min(listofnumberofpancakes))
print(' and '.join(top_eaters), "ate the most pancakes")
print(' and '.join(bottom_feeders), "ate the least pancakes")

Then the code prints:

The max of pancakes eaten by a person was:  3
The minimum of pancakes eaten by a person was:  1
charlie and dan ate the most pancakes
alice ate the least pancakes
Sign up to request clarification or add additional context in comments.

Comments

0

You can solve this problem by storing all the names with maximum in a list and then printing them. Essentially change this:

    if j == max(listofnumberofpancakes):
        print listofnames[i], "ate the most pancakes"

to something like this:

    if j == max(listofnumberofpancakes):
        max_list.append(listofnames[i])

Print this list after the loop statement with whatever else you want to print. You can loop over it to print in a specific format. That's your choice.

Comments

0

Instead of printing the name in the for loops at the bottom you could instead add these names to separate lists and iterate over these with some logic for printing:

maxList = []
minList = []

for i,j in enumerate(listofnumberofpancakes):
   if j == max(listofnumberofpancakes):
      maxList.append(listofnames[i])

if len(maxList) == 1:
   print maxList[0] + " ate the most pancakes"
else:
    print " and ".join(maxList) + " ate the most pancakes"

And similarly for the minList...

"string".join(list) will concatenate each element in a list with the string provided inside of the quotes acting like the glue.

3 Comments

You don't actually need a separate line for the case of only 1 item in maxlist. The join method automatically accounts for that (see my answer).
I did not know that. Thanks!
You bet. That's what makes it such a powerful method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.