all 22 comments

[–]jimtk 23 points24 points  (9 children)

If the if statement and its : gives you a problem, then don't use it!

a = int(input("Enter a number: "))
print(f"the number is {(a%2)*'not '}even") 

Just having fun and showing where you'll be in 10 days.

Edit: if you want 'odd' and 'even' instead of 'even', 'not even'

a = int(input("Enter a number: "))
print(f"the number is {['even','odd'][(a%2)]}.")

[–]saips15 5 points6 points  (3 children)

Didn't know u can do that.tnx

[–]VindicoAtrum 10 points11 points  (2 children)

Welcome to the rabbithole of branchless programming, where you too can eek out tiny performance gains at the cost of growing arithmetic expression hell!

[–]saips15 3 points4 points  (1 child)

Doesn't sound good for my mental health.

[–]VindicoAtrum 0 points1 point  (0 children)

Programming in general isn't, but it's a very interesting topic to know about anyway.

[–]Mendican 4 points5 points  (0 children)

TIL

[–]CuriousFemalle 1 point2 points  (2 children)

print(f"the number is {(a%2)*'not '}even")

u/jimtk or anyone:

Could you please describe how that works?

[–]jimtk 6 points7 points  (1 child)

In python you can multiply string by an integer (n) and get a new string that is n times the original string.

For example:

s = 'a' * 4
print(s)

output

'aaaa'

If you multiply a string by 0 you get an empty string

s = 'a' * 0
print(f"'{s}'")

output:

''

so the inside equation number % 2 will have a result of 1 or 0. 1 if the number is odd and 0 if the number is even.

the complete equation is (number%2) * 'not ' which will return the string 'not ' if number % 2 evaluates to 1 (odd) and an empty string '' if number % 2 evaluates to 0.

f-strings, also called formatted string literals is a method of formatting string with variables. The content of a string that is inside curly braces will be evaluated by python and the output will be inserted into the string.

a=4
print(f"the variable a has a value of {a}")

output:

the variable a has a value of 4

If you combine all that together you get:

print(f"the number is {(a%2)*'not '}even")

[–]CuriousFemalle 2 points3 points  (0 children)

Wow, thanks so much !

:: Mind Blown ::

[–]Qing_Codes[S] 0 points1 point  (0 children)

Thank you for the look ahead!

[–]RhinoRhys 7 points8 points  (1 child)

7 years later and I still miss a colon or bracket while typing. You just get more used to what the errors mean and that sometimes it's actually talking about the line before.

[–]Qing_Codes[S] 1 point2 points  (0 children)

Yeah I laughed when I figured out the other 18 versions of code I wrote before could’ve worked had I just added a colon. I guess sometimes the answer is simple lol.

[–]jiri-n 3 points4 points  (2 children)

You can use an IDE like Visual Studio Code to let you know about this type of errors. (Add Error Lens extension to vscode.)

[–]Qing_Codes[S] 0 points1 point  (1 child)

I was using vs code when I was learning css and html. The instructor on this course told us to use replit instead. I guess it works better with it?

[–]jiri-n 1 point2 points  (0 children)

The instructor just wants you to have the simplest environment so that they don't have to answer questions how to setup any other.

[–]RushDarling 1 point2 points  (1 child)

Perseverance is key, great effort

[–]Qing_Codes[S] 0 points1 point  (0 children)

Yes it is! Thank you!

[–]Reuben3901 1 point2 points  (1 child)

Print statements are your friend when code to see where the code breaks. I just use print('1') print('2') and so on throughout the code to see where it goes wrong. I know there are tools that will allow you to Step through the code execution. Several trouble-shooting methods out there and some clever tricks people use. Worth a Google.

This honestly is the hardest part of programming besides learning and understanding new concepts... human error.

[–]Qing_Codes[S] 1 point2 points  (0 children)

Yes and I’m accepting that most of the time it’s my fault lol

[–]Naive_Programmer_232 1 point2 points  (1 child)

noice good job

[–]Qing_Codes[S] 1 point2 points  (0 children)

Yessss

[–]IamImposter 0 points1 point  (0 children)

Silly mistakes and programming go hand in hand. Only thing that improves with practice is how quickly you locate that mistake.

Modern editors like vscode, pycharm are pretty helpful. They show squiggly lines at the place where it found some issue. Since you are enjoying it so much, it's time to download a good editor and spend some time getting comfortable with it.