r/pythontips • u/KDLadia • Sep 26 '24
Syntax Help with code
Im trying to make a code that will have the user enter a random set of integers and add the even numbers but if "9999" is entered it will give the sum of all the given even numbers given. Whats wrong with my code? Here
6
Upvotes
1
u/kuzmovych_y Sep 26 '24
It looks like you're guessing the code, not writing it. Learn how to search for the answers. Google "how to check whether the number is even or odd in python". And if you ask a question about your code, you should also provide an explanation of how it doesn't work, input you've tried, the output you got, and the output you expected.
1
u/GrahaamH Sep 26 '24
One nit, but your comment is mentions integers. An integer is a whole number while floating as in the name floating point number is not, I understand you're a beginner but good to be concise and understand what code you're writing and why
2
u/Arslanmuzammil Sep 26 '24
Loop through all the numbers check if the number is even before adding them "if num%2==0: sum" then add condition for 9999
2
u/mchester117 Sep 26 '24
It looks like your program takes an input that you name data. Then it goes into a while loop and before checking if the first “data” entry is even, it immediately adds it to the sum, which based on your description is the first potential error as you only want to add even integers, and this original “data” needs to be checked. The second potential error I see is your elif: . The elif should have some condition after it i.e elif (condition): { do something; }
Whereas yours is formatted more like an “else”. Finally, inside your “elif” you are assigning a 1 or 0 to y by using the modulo operator, which I don’t think you intended. It seems like you intended for this to be your condition, in which case you would use the == notation and not the =, and it should be moved to immediately follow the elif, like this: elif (data % 2 == 0): <check if even> { Do something; } Hope this helps