+ 4
Duty Free fails on test 3 (python)
I don't understand why my code fails Test Case no 3. I know, that my code isn't very optimized, but I try to understand it step by step. import re prices = input() space = " " prices_split = re.split(space, prices) all_float = [] for i in range (0, len(prices_split)): all_float.append(round(float(prices_split[i]) * 1.1)) terminal = True for i in range (0, len(all_float)): if all_float[i] > 20: terminal = False break if terminal == False: print("Back to the store") else: print("On to the terminal") https://www.sololearn.com/compiler-playground/c3tgTr9ZQUd3
6 Respostas
+ 2
Agnieszka Fietkiewicz-Zapalska ,
I got all 6 test cases to pass when I ran your code, not sure why it fails on the 3rd test case for you.
It may be due to some rounding error, since your converted prices rounds to the nearest integer, when it should round to 2 decimal places (whole currency value). Try adding the parameter in the `round` function, like this:
...round(float(prices_split[i]*1.1, 2))
Besides that, here are 2 suggestions to optimize/clean your code without changing it's logic:
1. Using regex module works, but an even simpler way is using the built-in string method `split`, which splits on whitespace by default:
prices_split = input().split()
2. Items in a list can be accessed iteratively without indexing:
for s in prices_split:
x = round(float(s)*1.1, 2)
all_float.append(x)
It can also help to break a long statement into several for clarity. Here `s` represents an item in the list (price as string), and `x` is the converted currency (as float).
Hope this helps :)
+ 8
Agnieszka Fietkiewicz-Zapalska ,
jusf for your information a version that uses the *all()* function to check the required condition:
inp = list(map(float, input().split( ))) # split the input at spaces and convert it to float
inp = [i * 1.1 for i in inp] # increment all elements of the inp list by multiplying with 1.1
if all(item < 20 for item in inp): # checks if all elements meet the criteria < 20
print('On to the terminal')
else:
print('Back to the store')
+ 3
Agnieszka Fietkiewicz-Zapalska It can help people to debug your code if, instead of copying and pasting it, you add a link to your code in Code Playground - use "+" button for that.
Also, if you do this into the question description, you just have to do it once, and whoever checks it will always see the updated version.
+ 2
# Hi, Agnieszka Fietkiewicz-Zapalska !
# You can compare your code with this one, if you want:
maxCost, rt = 20, 1.1
costs = (int(float(s))*rt for s in input().split())
if max(costs) > maxCost:
print("Back to the store")
else:
print("On to the terminal")
+ 2
@Per Bratthammar, thanks for your solusion. It' shorter and it works in all 6 Test Cases :)
@Mozzy, thank you for your explanation - I understand much more now. But, even your suggestions, my code is still not working on Test Case no 3... I don't understand why, but without knowing the content of the test, I have no idea what is bad :(
I've used another solution (for example from @Per Bratthammar), so the task has been completed.
0
Try this it will pass all the test case:
a=input()
b=a.split(" ")
lst=[]
for i in b:
lst.append(float(i)*1.1)
count=0
for i in lst:
if i>=20:
print(i)
count+=1
print(lst)
if count>=1:
print("Back to the store")
else:
print("On to the terminal"