# Thread: Quick Question on Python

1. ## Quick Question on Python

This is the question I'm having trouble with

Write a function pair_swap(pair) that takes a two-element tuple pair as a parameter and returns a new tuple consisting of the elements of pair swapped.

For example:

print pair_swap((10, 20)) should output (20, 10)

print pair_swap(("John Smith", 3512344)) should output (3512344, 'John Smith')

2. ## Re: Quick Question on Python

Snakes in planes?

3. ## Re: Quick Question on Python

This appears to be a homework question. Where are you getting stuck?

Simply posting the question with no additional data is essentially just asking us to do your homework for you...

4. ## Re: Quick Question on Python

Originally Posted by darkstar09
This is the question I'm having trouble with

Write a function pair_swap(pair) that takes a two-element tuple pair as a parameter and returns a new tuple consisting of the elements of pair swapped.

For example:

print pair_swap((10, 20)) should output (20, 10)

print pair_swap(("John Smith", 3512344)) should output (3512344, 'John Smith')
So are these print pair_swap equations not actually working as I don't really follow what your asking

5. ## Re: Quick Question on Python

Originally Posted by Erayd
This appears to be a homework question. Where are you getting stuck?

Simply posting the question with no additional data is essentially just asking us to do your homework for you...
I've gone through and checked the dir(tuple) but I'm not quite sure what functions I should be using to achieve the required output. A friend suggested we change the tuple to a list and then try reversing it then converting back to a tuple but I don't think thats been covered yet. Are we on the right track or is there another way to solve this? I've had a quick flick through the text book but haven't really found an answer and I'm studying for a different test tomorrow so had to give up for the night. I managed the other 16 questions quite easily, just stuck on how to solve this one

Originally Posted by gary67
So are these print pair_swap equations not actually working as I don't really follow what your asking
I need to create a function and then run the examples to give the output shown.

6. ## Re: Quick Question on Python

Originally Posted by darkstar09
...but I'm not quite sure what functions I should be using to achieve the required output. A friend suggested we change the tuple to a list and then try reversing it then converting back to a tuple but I don't think thats been covered yet.
Half on the right track, half on the wrong track.

You don't need to convert it to a list, but because tuples are immutable you can't convert it in place. Reversing it is definitely a good path to follow .

This whole problem can be done in a single (short) line of code; you don't need anything particularly complex.

7. ## Re: Quick Question on Python

Ah that was easy! Just trying to overcomplicate a simple thing! One more question I've got from another quiz on basic formatting.

The Question:
Using the str.format method and without using the global format function, write a function format_row(rank, name) that takes an integer rank in the range 0 - 9 and a string name of less than 18 characters in length and returns a 24-character result string as follows:

The result string starts and ends with '|'
Following the starting '|' there's a space character, the 1-digit rank, a period (i.e., '.') and another space.
The name follows the rank field (item 2) and is left justified.

This function might be used in, for example, a tabular display of sportspeople rankings.
My code:

Code:
```template = '| {0}. {1}|'
def format_row(rank,name):
return template.format(rank, (format(name, "18")))

print format_row(1, 'Fred Bloggs')

print format_row(9, 'Michael Angus Ng')

print format_row(2,'abcdefghijklmnopqr')```
The output:
| 1. Fred Bloggs |
| 9. Michael Angus Ng |
| 2. abcdefghijklmnopqr|
What the quiz tells me when I submit it:
def format(obj, fmt): assert False, 'Illegal use of the global format function'
print format_row(2,'abcdefghijklmnopqr')
I must be overlooking something surely as my output is exactly what the quiz shows? Or is it something to do with the global format function?

8. ## Re: Quick Question on Python

Originally Posted by darkstar09
Or is it something to do with the global format function?
Well the question tells you not to use it ("...without using the global format function..."), and you are using it - so you need to come up with a different solution that follows the instructions correctly .

9. ## Re: Quick Question on Python

Originally Posted by Erayd
Well the question tells you not to use it ("...without using the global format function..."), and you are using it - so you need to come up with a different solution that follows the instructions correctly .
Back to reading the text book then! I'll let you know how I get on.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•