+ 5

[solved] Problems with solving if statement V task

Hi coders as the title says I tried to solve the code coach problem of the if statement section with is called if statement V. This is declared as a easy task and I tried it several times but test case 4 (hidden input) always fails for me. Any ideas? The task is like that: if Statements +10 XP Let's imagine you want to buy an ice-cream for 10 of your friends. Write a program that will take the money you have and the price of one ice-cream, and will output the remaining money only if you can buy that ice-cream for your all 10 friends. Sample Input 80 7 Sample Output 10 Explanation 7*10 = 70. 10 is remaining money (80-70). My code so far is money = int(input()) price = int(input()) ausgabe = price * 10 if ausgabe <= 100: if ausgabe <= money: print(money - ausgabe)

5th Sep 2020, 8:39 PM
Stefan
Stefan - avatar
17 Answers
+ 1
''' One if statement should be enough ''' money = int(input()) price = int(input()) price*=10 if (money-price)<0: print() else: print(money-price)
5th Sep 2020, 8:51 PM
Steven M
Steven M - avatar
+ 5
Serana Zentha also thank you for you help although the code is solved. But with your explanation was somehow changing my thinking about this. So in the future I will make sure to think about even more possible input since I never thought about a negative value in this case. But I guess that is your job as a programmer to handle the most stupid user input🤣
22nd Oct 2020, 7:03 PM
Stefan
Stefan - avatar
+ 4
The reason why your code isnt working is because in test case 4, the numbers that are given print a negative number, but do not exceed the greater than 100 limit. Since the code does not check if the price or money is a negative number, the code will still execute, the code does not tell you this as of course no one in their right minds would think someone would enter a negative number. However, when it comes to code, it is important to think outside the box so that, as coders, we are able to leave out as little glitches and errors as possible, if that makes sense
22nd Oct 2020, 4:42 PM
Serana Zentha
Serana Zentha - avatar
+ 3
So thank you very much it worked your way. But except the number of if starments I don't understand what is the difference.
6th Sep 2020, 1:34 PM
Stefan
Stefan - avatar
+ 2
To further elaborate, test case 4 wants to make sure you do not print the total if it is a negative outcome, as you cant have negative money (unless you overdraft your bank account, lol)
22nd Oct 2020, 4:43 PM
Serana Zentha
Serana Zentha - avatar
+ 2
Chey Brown no prob, coding i rough, i reccomend doing what modt coders do and having a rubber duck, stuffed animal, etc, to talk through as to why the code isnt working, you slowly begin to realize once you talk to someone. Coding is a team sport, imo, you can never be as successful as possible when you do it alone.
22nd Oct 2020, 4:50 PM
Serana Zentha
Serana Zentha - avatar
+ 1
I had the same issue as Stefan, please could explain the difference between your code Steven M and his please
11th Oct 2020, 1:54 AM
Chey Brown
Chey Brown - avatar
+ 1
I wish I could give more than one upvote
22nd Oct 2020, 4:44 PM
Chey Brown
Chey Brown - avatar
+ 1
Chey Brown the answer for the problem given by Steven M Steven M helped me to figure it out, as he makes sure the price does not underflow 0, which makes perfect sense as to why test case 4 is a total failure
22nd Oct 2020, 4:46 PM
Serana Zentha
Serana Zentha - avatar
+ 1
I was literally about to ask how you figured out what test case 4 is
22nd Oct 2020, 4:47 PM
Chey Brown
Chey Brown - avatar
+ 1
This will help my progress so much. I've come a long way since then though. Been solving the hard ones. Thank you soooo much though
22nd Oct 2020, 4:48 PM
Chey Brown
Chey Brown - avatar
+ 1
Sorry, but the solution here is wrong, although it could have passed the test cases. let's see user input are 90 and -4, we have output 130 as the solution provided here. The correct one should be like this: (let's consider user may input letter, negative number, floating number as well...) try: money = float(input()) price = float(input()) price = 10*price if money >= price and money >=0 and price >=0 : print(money-price) else: print() except ValueError: print() Unfortunately, I've changed the float to int, but it couldn't pass for no reasons?
30th Nov 2020, 1:43 AM
Jingyi Zhao
Jingyi Zhao - avatar
+ 1
The objective is to write a script that passes all test cases, this code passes all test cases. The values you have suggested, negative 4 dollars per ice cream, doesn't make any sense. With this logic you are being paid 4 dollars per ice cream. The try/except block is a nice addition, but it is not needed. I think your code is not working because the expected output is an int, not a float.
30th Nov 2020, 2:04 AM
Steven M
Steven M - avatar
+ 1
try/except is needed, when user input a letter.
30th Nov 2020, 2:58 AM
Jingyi Zhao
Jingyi Zhao - avatar
+ 1
Try this code: if money>=0 and price>=0: remainder = money-total if remainder>=0 and remainder<=100: print(remainder)
30th Nov 2020, 5:07 PM
Amit Kumar
Amit Kumar - avatar
0
They are almost the same... if ausgabe <= 100: Remove that line and everything in Stefan Kunert code should work. There are multiple ways to solve this problem. The solution i have proposed is one way
11th Oct 2020, 2:34 PM
Steven M
Steven M - avatar
0
Thanks! that's really helpful to know that there's not just one way
11th Oct 2020, 2:54 PM
Chey Brown
Chey Brown - avatar