Hej there,

I am writing a little throw away program in order to better understand
how I can loop through a variable and a list at the same time. Here's
what the program does and how it looks like: It counts the number of
backpackers (assuming a growth rate of 15 % year on year) over the
last five years:

Backpackers = 1000000
for x in range(2009, 2014):
    Backpackers = Backpackers*1.15
    print("In %d there were %d backpackers worldwide." % (x, Backpackers))

>>>
In 2009 there were 1150000 backpackers worldwide.
In 2010 there were 1322500 backpackers worldwide.
In 2011 there were 1520874 backpackers worldwide.
In 2012 there were 1749006 backpackers worldwide.
In 2013 there were 2011357 backpackers worldwide.

Now I want to enhance that program a bit by adding the most popular
country in each year. Here's what I want to get as the output:

>>>
In 2009 there were 1150000 backpackers worldwide and their most
popular country was Brazil.
In 2010 there were 1322500 backpackers worldwide and their most
popular country was China.
In 2011 there were 1520874 backpackers worldwide and their most
popular country was France.
In 2012 there were 1749006 backpackers worldwide and their most
popular country was India.
In 2013 there were 2011357 backpackers worldwide and their most
popular country was Vietnam.

I assume that I need to have a list like this:

PopularCountries = ["Brazil", "China", "France", "India", "Vietnam"]

But I struggle to modify the program above in a way that it loops
properly through "Backpackers" and "PopularCountries".
>From all my iterations there's only one that came at least halfway
close to my desired result:

PopularCountries = ["Brazil", "China", "France", "India", "Vietnam"]
Backpackers = 1000000
for x in range(2009, 2014):
    Backpackers = Backpackers*1.15
    PopularCountries = PopularCountries.pop()
    print("In %d there were %d backpackers worldwide and their most
popular country was %s." % (x, Backpackers, PopularCountries))

It loops only once through "Backpackers" and "PopularCountries"
(starting with the last item on the list though) and then it breaks:

>>>
In 2009 there were 1150000 backpackers worldwide and their most
popular country was Vietnam.
Traceback (most recent call last):
  File "C:/Users/Rafael_Knuth/Desktop/Python/Backpackers.py", line 6,
in <module>
    PopularCountries = PopularCountries.pop()
AttributeError: 'str' object has no attribute 'pop'

My questions:
Is there a way to use pop() to iterate through the list in a correct
order (starting on the left side instead on the right)?
If not: What alternative would you suggest?
Do I need to rewrite the program in order to iterate through
"Backpackers" and "PopularCountries" at the same time? (for example
using a while instead of a for loop?)
Or is there a way to modify my existing program?
Should "PopularCountries" be a list or do I need a dictionary here?

I am using Python 3.3.0.

Thank you in advance!

All the best,

Rafael
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to