Pycharm says "simplify chained comparison". I dont know how.

Joined
Jul 20, 2023
Messages
56
Reaction score
2
Im writing a rock paper scissor program and here is the block that its refering to:
Python:
if to_check > 0 and to_check < 4:
    match(to_check)
else:
    clear_screen()
    print('Select a number between 1 and 3')
    input('Press ENTER to continue ')
    clear_screen()
    main_menu()

Its telling me to simplify the if statement.
if x > 0 and x < 4:

I dont see how to do that???
 
Joined
Jul 4, 2023
Messages
507
Reaction score
63
Try like this .. ;)

Python:
if 0 < x < 4:


Python:
def isInRange(x):
    if 0 < x < 4:
        return True
    return False

for test in range(-1, 6):
    print(test, str(isInRange(test)))
 
Joined
Jul 20, 2023
Messages
56
Reaction score
2
Try like this .. ;)

Python:
if 0 < x < 4:


Python:
def isInRange(x):
    if 0 < x < 4:
        return True
    return False

for test in range(-1, 6):
    print(test, str(isInRange(test)))
Wow that is a really interesting syntaxes for an if statement that I didn't know existed. But I'm not really a fan of it to be honest. At least for this case I think the range is the best way to go. But that would be a good fit for another situation idk. Either way pretty cool. I'm glad I now know about it.

i also checked to see if you can keep adding on to that ex. If x > y < z < i:
And I see that you can.

It's funny because I always wished there was a shorter way to compare 1 variable in multiple ways without having to keep writing it over and over.
Ex. If x ... and x ... and x... :

Is there a shorter way of doing that wether it be a string and int or whatever and wether is in an if statement of a while loop, whatever...
 
Last edited:
Joined
Jul 4, 2023
Messages
507
Reaction score
63
Python can operate with very interesting syntax e.g

Python:
x = 1
# isInRange = (False, True)[0 < x < 4]
# print(isInRange)
print((False, True)[0 < x < 4])

Python:
fruit = 'BANANA'
print(*fruit)           #  B A N A N A
print(*fruit, sep='.')  #  B.A.N.A.N.A
print([*fruit])         #  ['B', 'A', 'N', 'A', 'N', 'A']

Python:
a = { 'lorem1': 1, 'impsum1': 2, 'dolor1': 3, 'amet1': 4 }
b = { 'lorem2': 1, 'impsum2': 2 }
c = { **a, **b }
 
print(c)

Python:
txt = 'PyTHoN'
 
print(f'{txt:*<40}')
print(f'{txt:*>40}')
print(f'{txt:*^40}')
 
for i in range(len(txt) + 1, 41):
    print(f'{txt:*>{i}}')

and My favorite [ on-line ] ;)
Python:
count_to_10       = [ '1','2','3','4' '5','6','7','8','9','10' ]
print(len(count_to_10))
 
count_to_10_again = [ '1','2','3','4','5','6','7','8','9','10' ]
print(len(count_to_10_again))
 
Joined
Jul 20, 2023
Messages
56
Reaction score
2
Hello !



Python:
if number in range(1,4)


use a range checking with your 'if'
I think this is probably what it was looking for. Interesting enough that it would fuss over something like that. Guess it's just going by the book.
 
Joined
Jul 20, 2023
Messages
56
Reaction score
2
Python can operate with very interesting syntax e.g

Python:
x = 1
# isInRange = (False, True)[0 < x < 4]
# print(isInRange)
print((False, True)[0 < x < 4])

Python:
fruit = 'BANANA'
print(*fruit)           #  B A N A N A
print(*fruit, sep='.')  #  B.A.N.A.N.A
print([*fruit])         #  ['B', 'A', 'N', 'A', 'N', 'A']

Python:
a = { 'lorem1': 1, 'impsum1': 2, 'dolor1': 3, 'amet1': 4 }
b = { 'lorem2': 1, 'impsum2': 2 }
c = { **a, **b }
 
print(c)

Python:
txt = 'PyTHoN'
 
print(f'{txt:*<40}')
print(f'{txt:*>40}')
print(f'{txt:*^40}')
 
for i in range(len(txt) + 1, 41):
    print(f'{txt:*>{i}}')

and My favorite [ on-line ] ;)
Python:
count_to_10       = [ '1','2','3','4' '5','6','7','8','9','10' ]
print(len(count_to_10))
 
count_to_10_again = [ '1','2','3','4','5','6','7','8','9','10' ]
print(len(count_to_10_again))
That first example is pretty cool. And the last one... please explain to me what is going on here. I feel like that could really screw a guy up in a project.
 
Joined
Jul 20, 2023
Messages
56
Reaction score
2
Python can operate with very interesting syntax e.g

Python:
x = 1
# isInRange = (False, True)[0 < x < 4]
# print(isInRange)
print((False, True)[0 < x < 4])

Python:
fruit = 'BANANA'
print(*fruit)           #  B A N A N A
print(*fruit, sep='.')  #  B.A.N.A.N.A
print([*fruit])         #  ['B', 'A', 'N', 'A', 'N', 'A']

Python:
a = { 'lorem1': 1, 'impsum1': 2, 'dolor1': 3, 'amet1': 4 }
b = { 'lorem2': 1, 'impsum2': 2 }
c = { **a, **b }
 
print(c)

Python:
txt = 'PyTHoN'
 
print(f'{txt:*<40}')
print(f'{txt:*>40}')
print(f'{txt:*^40}')
 
for i in range(len(txt) + 1, 41):
    print(f'{txt:*>{i}}')

and My favorite [ on-line ] ;)
Python:
count_to_10       = [ '1','2','3','4' '5','6','7','8','9','10' ]
print(len(count_to_10))
 
count_to_10_again = [ '1','2','3','4','5','6','7','8','9','10' ]
print(len(count_to_10_again))

What is going on with that last example? I know python starts counting from 0 so why the difference on the second list?
 
Joined
Jul 20, 2023
Messages
56
Reaction score
2
not starts counting, starts indexing from 0 ;)

INDEX [ 0, 1, 2, 3, 4, 5 ]
ARRAY [ 1, 2, 3, 4, 5, 6 ]
COUNT [ 1, 2, 3, 4, 5, 6 ] # len(ARRAY)

So an array doesn't start at 0? Furthermore I thought python didn't have arrays or rather its just called a list instead.
 
Joined
Jul 4, 2023
Messages
507
Reaction score
63
I know that python didn't have arrays.
I used ARRAY more as name of variable for demonstration purpose, maybe a bit unfortunate, because it is misleading. ;)

Python:
demo_list = { 1, 2, 3, 4, 5, 6 }
print(len(demo_list), type(demo_list), '\n')
for index, value in enumerate(demo_list):
    print(index, value)

Python:
demo_tuple = ( 1, 2, 3, 4, 5, 6 )
print(len(demo_tuple), type(demo_tuple), '\n')
for index, value in enumerate(demo_tuple):
    print(index, value)

Python:
demo_set = { 1, 2, 3, 4, 5, 6 }
print(len(demo_set), type(demo_set), '\n')
for index, value in enumerate(demo_set):
    print(index, value)

any way INDEX start from 0
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,056
Messages
2,570,443
Members
47,089
Latest member
Bobby2025b

Latest Threads

Top