0

Could any one help me to debug the following python code?

code is shown here:

#!/usr/bin/python
# Filename: using_tuple.py


zoo = ('python', 'elephant', 'penguin') # remember the parentheses are optional
print('Number of animals in the zoo is', len(zoo))

new_zoo = ('monkey', 'camel')
print('Number of cages in the new zoo is', len(new_zoo))
print('All animals in new zoo are', new_zoo)
print('Animals brought from old zoo are', new_zoo[2])
print('Last animal brought from old zoo is', new_zoo[2][2])
print('Number of animals in the new zoo is', len(new_zoo)-1+len(new_zoo[2]))
4
  • 1
    There is no connection between zoo and new_zoo. Assuming at least one animal has to be brought from zoo to new_zoo. Also you have indexes out of bounds as python use a zero based index where the first item is [0] Commented Feb 12, 2011 at 11:03
  • 3
    At least two things need to be clarified: first, whether 'python', 'elephant', and 'penguin' belong to the new zoo, that is, they have been brought from the old zoo; second, whether those three animals have been put in a single cage in the new zoo. Calculations and tuple accesses in print() calls are contradictory, swinging between different models of the new zoo. Without a further explanation of how you intend to model the new zoo with respect to the old zoo, it's quite impossible to guess exactly what the correct answers should be. Commented Feb 12, 2011 at 11:35
  • 3
    Is this your answer to the homework, or is the homework just to make this code work properly? Commented Feb 12, 2011 at 13:04
  • 1
    It would help to see the homework problem statement. Commented Feb 12, 2011 at 17:23

5 Answers 5

2

Where do you combine old and new zoo?

Possibilities:

>>> new_zoo = ('monkey', 'camel', zoo)
>>> new_zoo
('monkey', 'camel', ('python', 'elephant', 'penguin'))
>>> new_zoo = ('monkey', 'camel') + zoo
>>> new_zoo
('monkey', 'camel', 'python', 'elephant', 'penguin')
Sign up to request clarification or add additional context in comments.

2 Comments

I don't mean to combine old and new zoo. It happed that I can not successfully run these code.
@jervis the statement new_zoo[2][2] really suggests you assume option 1, that is, put zoo into new_zoo.
2

Indexing in programming languages usually starts from zero, not one. The length maybe 2, but the second element is with the index 1.

Comments

0

You forgot to add the old zoo to the new zoo.

Comments

0

In this line: `print('Last animal brought from old zoo is', new_zoo[2][2])'

new_zoo[2][2] is invalid in your current code for 2 reasons: 1. Arrays are 0-indexed. new_zoo[2] refers to the 3rd element, not the 2nd. 2. Also, you will just be printing a character (specifically the 3rd character) of that zoo animal.

Comments

-1

The variable new_zoo has two elements and the number of elements starts from zero

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

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.