#Python - Plane Tickets

57 messages · Page 1 of 1 (latest)

viscid patrol
#

Hi. I have a problem with first task in this exercise. Rn my code looks like this
letters = ['A', 'B', 'C', 'D'] for letter in letters: if number > 0: yield letter number -= 1
But this code doesn't pass all tests. Do you have any suggestions?

static stratus
#

Which tests don't pass? What is the output?

viscid patrol
#

can i send ss?

static stratus
#

What is ss?

#

You can copy paste your error here

viscid patrol
#

I mean screenshot😅

static stratus
#

Please don't!

#

Codeblocks are much easier to work with. And read.

viscid patrol
#

sure

#

`"""Test if generate_seat_letters() returns a generator type."""

number = 5
error_message = (f'Called generate_seat_letters({number}). '
f'The function returned a {type(generate_seat_letters(number))} type, '
f"but the tests expected the function to return a <class 'generator'> type.")

self.assertTrue(inspect.isgenerator(generate_seat_letters(number)), msg=error_message)`

#

this is test 2

#

and here test 3:

static stratus
#

Let's take one test at a time

viscid patrol
#

`AssertionError: Lists differ: ['A', 'B', 'C', 'D'] != ['A', 'B', 'C', 'D', 'A']

Second list contains 1 additional elements.
First extra element 4:
'A'

  • ['A', 'B', 'C', 'D']
  • ['A', 'B', 'C', 'D', 'A']
    ? +++++
    : Called generate_seat_letters(5). The function returned ['A', 'B', 'C', 'D'], but the tests expected ['A', 'B', 'C', 'D', 'A'] when generating 5 seat(s).`
static stratus
#

Yeah. That looks good!

#

Do you understand what that test is calling?
Do you understand what the expected output is and why?
Do you understand what your code generates and why?

#

?

viscid patrol
#

Do you understand what that test is calling? Yes
*Do you understand what the expected output is and why? * I think yes.
Do you understand what your code generates and why? For every letter in list I check if the given number is greater than zero and if yes I yield that letter and dicrease the number by one. But I dont know why it doesnt print this last 'A'

static stratus
#

What are you looping over? How many loop iterations does your for have?

viscid patrol
#

okaay, so my code doesn't include this last 'A' because my for loop ends on the last element which is D, right?

static stratus
#

That sounds about right to me

#

Do you need help figuring out how to work around that?

viscid patrol
#

I think yes

static stratus
#

You know how many letters need to be generated, right?

viscid patrol
#

yes 5

static stratus
#

Or number 😉

#

That suggests you need to yield number times, yes?

viscid patrol
#

yea

static stratus
#

How would you go about running a yield line number times?

viscid patrol
#

Maybe something like that:

#

letters = ['A', 'B', 'C', 'D'] current_letter = 0 for num in range(number): if current_letter < 4: yield letters[current_letter] current_letter += 1 else: current_letter = 0

static stratus
#

That should probably work. Though the for loop already gives you a loop variable. Do you need two or can you reuse that?

viscid patrol
#

I don't think so because when I'll use num for index in letters it will give me out of range error

static stratus
#

Are you familiar with modulo arithmetic?

viscid patrol
#

yup

static stratus
#

Can that solve this issue?

viscid patrol
#

sooo, num % 4?

#

no

#

it wont work

static stratus
#

Why would or wouldn't it work?

#

(There's also always the option of just trying it and seeing 😄 )

#

(But if you go that route, make sure you understand what's happening!!)

viscid patrol
#

yoo

#

thanks for help

#

now it works

#

letters = ['A', 'B', 'C', 'D'] for num in range(number): yield letters[num % 4]

#

and it's much shorter

#

You think it is okay?

static stratus
#

🎉

#

It's solid. I'd make slight changes to it but that's probably better left to a code review/mentor session.

viscid patrol
#

okay

#

this topic disappear or I need to somehow delete it?

static stratus
#

We ask that people apply a ✅ (:white_check_mark:) to the top post to indicate this help request has been resolved.

viscid patrol
#

like that?

static stratus
#

Yup! Thanks!

viscid patrol
#

Thanks again for your help. I'll try to remember that if I don't know how to do something, ask myself questions 😉

static stratus
#

You're very welcome 😄