+ 1

Python Data Structure Course

I've tried to solve the third project on python data structure course This is the task You are analyzing sales data from a ticket office. The ticket for an adult is $20, while the ticket for a child under 18 is $5. The data you are given is in a dictionary format, where the keys are the sold ticket numbers, and the values are the customer ages. For example, "123-08": 24 means that the ticket was bought a 24 year old. Your goal is to calculate how much more money the office would make if it would change the ticket discount age to the given input. So, your program needs to take an integer as input and output the percentage of revenue growth, if the discount was given to people under that age. For example, if the office made $15000 with the original discount age, and would make $18000 with 14 as the discount age, then the growth would be ((18000-15000)/15000)*100 = 20% So, for the input 14, your program should output 20. The output should be an integer (use int() to convert the result). and this is my code https://www.sololearn.com/ceBwyPajO9NR I solved all the cases but one hidden case I still cant find what's wrong with my code Please help me, this is the only one project that is not finished yet

16th Sep 2022, 2:26 AM
Muhammad Ramadhan Kadar
Muhammad Ramadhan Kadar - avatar
6 odpowiedzi
+ 2
Still I can't access it. Post it reply.. I try to modify to work it.. May you posting from web version. I can't copy more than 1024 characters. If you have mobile version, share better to save it share link here from app. edit: got it , wait https://code.sololearn.com/ceBwyPajO9NR edit: Don't define new variables in if block. Declare it out side if block.. Take these after loop : eighteen=len(normal_age) count_new_teenage=len(new_age)
16th Sep 2022, 9:40 AM
Jayakrishna 🇮🇳
+ 1
SL Web link currently not working.. Paste link in reply with removing 'compiler-playground'
16th Sep 2022, 8:08 AM
Jayakrishna 🇮🇳
+ 1
The link has been updated. Thank you I didnt know it should have been like that
16th Sep 2022, 8:40 AM
Muhammad Ramadhan Kadar
Muhammad Ramadhan Kadar - avatar
+ 1
BIG THX FOR YOU!!! 😭😭 If you dont mind, can you explain why it caused problem?
16th Sep 2022, 12:32 PM
Muhammad Ramadhan Kadar
Muhammad Ramadhan Kadar - avatar
+ 1
Muhammad Ramadhan Kadar It about variable scope issue. Declaring in a block, it's scope belong to that block only... (Python is some different..) Here : if c<18: normal_age.append(c) eighteen = len(normal_age) <= this variable is a new variable, only created when this block get executed. If there is no value c < 18 then eighteen variable will don't get created. You are using it later, so there it becomes undeclared variable so raise error. Declaring it outside if block, will not raise this issue. Variables created anyways... Same applies for c<age => count_new_teenage variable... Hope it clears now....
16th Sep 2022, 7:31 PM
Jayakrishna 🇮🇳
+ 1
list1 = [] #in this add all the values of data. list2 = [] #in this add all the values which are favourable for us according to new age. list3 = [] #in this add all the values according to 18 years age. for value in data.values(): list1.append(value) for value in data.values(): if value>=age: list2.append(value) for value in data.values(): if value>=18: list3.append(value) a = len(list1) b = len(list2) c = len(list3) d = a-b e = a-c total = ((b*20+d*5)-(c*20+e*5))/(c*20+e*5)*100 print(int(total)) #Hope it helped you:)
17th Sep 2022, 1:07 PM
Diksha Sharma
Diksha Sharma - avatar