+ 1

Please what's the error

It seems something is wrong with the code https://code.sololearn.com/ccatgaOn1v7g/?ref=app

14th Mar 2023, 10:37 PM
Uyi Oduware
Uyi Oduware - avatar
14 Respostas
+ 4
Ok, I took a look at the actual problem. Yourlogic is a little off. I think it will help if you do some nested if statements. Like this if(international==true){ if(amount*0.15>=50){ return 50 }else{ return amount*0.15 } }else{ if (amount >= 75.0){ return 0 }else{ return amount * 0.1 This makes it more clear, to me at least, which branch is international and which is domestic. Then, within there, we handle the shipping costs. Also, we don't need the cost variable anymore, and since that was being defined at multiple levels that was what was giving you the shadowing warning
15th Mar 2023, 12:47 AM
Bob
+ 2
I don't understand the purpose of your maximum variable here. I'm guessing your goal is if a domestic shipment over x amount then shipping is free. In the case the cost is 100. 0 your code is gonna say maximum is 15.0. Also since you never use cost for anything other than a return it makes more sense to have the return just be the calculation.
15th Mar 2023, 12:36 AM
Bob
+ 1
It was never used in the scope it was declared. When it was used it was in adifferent scope where it was changed to something else so it "shadows"the variable from the previous scope. I think it's worth looking for lectures or reading materials about scope(not just local/ global, but where a local scope begins and ends, putting a smaller local scope within a larger local, and lexical/dynamic scope), aliasing, and shadowing
15th Mar 2023, 12:14 PM
Bob
+ 1
Much better👍my only comment would be you decided to precalculate amount*.15, why recalculate it for the return in the 3rd branch of your conditional? Just return maximum there. But that's more stylistic, like my recommendation to nest the ifs
15th Mar 2023, 12:26 PM
Bob
0
Thanks alot, though i need do some nested if statement, but i had to remove the variable cost as you wrote. Why was cost never used?
15th Mar 2023, 8:04 AM
Uyi Oduware
Uyi Oduware - avatar
0
Thanks bro, I have put maximum in place of the calculation, since maximum contains the same calculation.
15th Mar 2023, 2:18 PM
Uyi Oduware
Uyi Oduware - avatar
0
Know any lecture, i can use to learn about scope, it will be helpful, thank you.
15th Mar 2023, 2:19 PM
Uyi Oduware
Uyi Oduware - avatar
0
coursera.org/learn/programming-languages/home/week/1 I'd recommend everyone that wants to get really into coding take this. It's going to be very unfamiliar compared to the programming style you've likely seen so far. It looks at functional vs OOP styles of programming and static vs dynamic typing. Really helps open your eyes to why different languages do different things better
15th Mar 2023, 6:17 PM
Bob
0
Ok, fill me in
15th Mar 2023, 6:20 PM
Uyi Oduware
Uyi Oduware - avatar
0
k = 20 m = 21 while k + m < 2021: k = k + 2 m = m - 1 How many iterations of the while-loop will happen?
16th Mar 2023, 8:02 PM
Vanshika
0
How does that help me Vanshika , plus what's written it doesn't even make sense. what's your point.
16th Mar 2023, 11:14 PM
Uyi Oduware
Uyi Oduware - avatar
0
Vanshika are you trying to ask your own question? If so please do it in is own post
16th Mar 2023, 11:34 PM
Bob
0
Ok
17th Mar 2023, 4:51 AM
Vanshika