#๐Ÿ”’ question

27 messages ยท Page 1 of 1 (latest)

iron jungle
#

my function keeps repeating an if function and idk why


letterinp = []
    num = []
    for c in input_str:
        letterinp.append(c)
    
    stackval = {'A':0,'B':1,'C':2,'D':3,'E':4,'F':5}

for i in range(0,2):
            for key, value in stackval.items():
                if letterinp[i] == key:
                    num.append(value)
                
        if len(letterinp) == 2:
            num.append(1)
        
        if len(letterinp) == 3:
            g = int(letterinp[2])
            if g in range(1,10):
                num.append(g)
       
        output = tuple(num)   
        return output

ik this probably isnt the most elegant way to do this but whatever it works, it takes an input like (AB2) and converts it into (0, 1, 2) which i need for later code. But i wanted to add a way to reset the code and undo it, so i added:


if letterinp == "r" or "R":
        print("reset")
        return 0
    
    elif letterinp == "u" or "U":
        print("undo")
        return -1
    
    else:
        for i in range(0,2): 
            for key, value in stackval.items():
                if letterinp[i] == key:
                    num.append(value)

where the else is the just the same code as above
however now no matter what i type in, whether its a valid input or "u" or "U", it always just says reset, its like its always triggering the reset part of the if and never moving on.

sharp cometBOT
#

@iron jungle

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

fleet dove
#

!or gotcha

sharp cometBOT
#
The or-gotcha

When checking if something is equal to one thing or another, you might think that this is possible:

# Incorrect...
if favorite_fruit == 'grapefruit' or 'lemon':
    print("That's a weird favorite fruit to have.")

While this makes sense in English, it may not behave the way you would expect. In Python, you should have complete instructions on both sides of the logical operator.

So, if you want to check if something is equal to one thing or another, there are two common ways:

# Like this...
if favorite_fruit == 'grapefruit' or favorite_fruit == 'lemon':
    print("That's a weird favorite fruit to have.")

# ...or like this.
if favorite_fruit in ('grapefruit', 'lemon'):
    print("That's a weird favorite fruit to have.")
fleet dove
#

!e py if True: print('A') if False: print('B') if 'abc': print('C') if '': print('D')

sharp cometBOT
fleet dove
#

Positive-length strings are "truthy". Zero-length strings are "falsy".

#

!e py print('A' or 'B') print('' or 'C')

sharp cometBOT
fleet dove
#

!e py print(True or 'A') print(False or 'B')

sharp cometBOT
fleet dove
#

@iron jungle

iron jungle
#

so now, thru the input i have 3 different outputs, 0 for reset, -1 for undo, or e.g. (1, 0, 2) for a valid input, but later parts of my code say like input[2] to access the last value of the input, and if i return 0 (for a reset) it gives an error because there is no [2] for input anymore. is there anyway to get around this or is the easiest way just putting my code in a big IF loop of smth like


if input != ['r'] or input != ['R'] or input != ['u'] or input != ['U']:

although i feel like thats a lil silly

fleet dove
#

Avoid shadowing the input name.

#

Okay, let's see.

#

Your expression here will always evaluate to True.

#

Because if it isn't ['r'] then it'll be something else, True. If it is ['r'], then it won't be ['R'], True.

#

@iron jungle

iron jungle
fleet dove
#

!e ```py
for key in 'rRuUX':
if key != ['r'] or key != ['R'] or key != ['u'] or key != ['U']:
print(key)

sharp cometBOT
fleet dove
#

You may as well write if True.

#

Or not have an if.

iron jungle
#

source = move[0]
    to = move[1]
    amount = move[2]
        
   
    if len(move) != 3:
        print("Invalid input")
        return False

for example this is the next part of my code, and if i input "r" which should reset it (havent implimented that yet) theres an issue with source = move[0] because r returns just 0

sharp cometBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.