#Python - Plane Tickets
57 messages · Page 1 of 1 (latest)
Which tests don't pass? What is the output?
can i send ss?
I mean screenshot😅
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:
Let's take one test at a time
`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).`
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?
?
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'
What are you looping over? How many loop iterations does your for have?
okaay, so my code doesn't include this last 'A' because my for loop ends on the last element which is D, right?
That sounds about right to me
Do you need help figuring out how to work around that?
I think yes
You know how many letters need to be generated, right?
yes 5
yea
How would you go about running a yield line number times?
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
That should probably work. Though the for loop already gives you a loop variable. Do you need two or can you reuse that?
I don't think so because when I'll use num for index in letters it will give me out of range error
Are you familiar with modulo arithmetic?
yup
Can that solve this issue?
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!!)
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?
🎉
It's solid. I'd make slight changes to it but that's probably better left to a code review/mentor session.
We ask that people apply a ✅ (:white_check_mark:) to the top post to indicate this help request has been resolved.
like that?
Yup! Thanks!
Thanks again for your help. I'll try to remember that if I don't know how to do something, ask myself questions 😉
You're very welcome 😄