#๐Ÿ”’ not complex stuff, but why does it ALWAYS return true?

70 messages ยท Page 1 of 1 (latest)

quaint gulch
#

im doing some stuff on a microbit for my class and we had to make basically a bop it kind of thing with it giving instructions and if you did them correctly it gives you a smile and if done wrong it gives you smth else. For some reason no matter what I do it always gave me a smile, even though I tried to make any other action fail.

# Imports go at the top
from microbit import *
import random as rand
import time

gen_numbers = {
    0: 'A',
    1: 'B',
    2: 'II',
    3: 'S'
}
case_true = False

# Code in a 'while True:' loop repeats forever
while True:
    case_true = False
    gen_num = rand.randint(0, 3)
    display.show(gen_numbers[gen_num])
    time.sleep(3.5)
    if gen_num == 0:
        if button_a.was_pressed:
            case_true = True
        elif any([button_b.was_pressed(), accelerometer.was_gesture('shake')]):
            case_true = False
    elif gen_num == 1:
        if button_b.was_pressed:
            case_true = True
        elif any([button_a.was_pressed(), accelerometer.was_gesture('shake')]):
            case_true = False
    elif gen_num == 2:
        if all([button_a.was_pressed, button_b.was_pressed]):
            case_true = True
        elif accelerometer.was_gesture('shake'):
            case_true = False
    elif gen_num == 3:
        if accelerometer.was_gesture('shake'):
            case_true = True
        elif any([button_b.was_pressed(), button_a.was_pressed()]):
            case_true = False
    if case_true == True:
        display.show(Image.HAPPY)
    elif case_true == False:
        display.show(Image.TARGET)
    time.sleep(0.90)
    ```
untold lakeBOT
#

@quaint gulch

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.

quaint gulch
#

Plus it should default to false anyway if I do nothing since it says case_true = false in the beginning, i just dont get it

young nest
#

u got help?

#

@quaint gulch

past tartan
#

like .was_pressed()

quaint gulch
#

those functions just check if it was pressed or not and return true if it did happen

#

i can usually just run stuff like
if button_a.was_pressed(): cuz its a boolean and i dont need to specify == True

young nest
#
# Imports go at the top
from microbit import *
import random as rand
import time

gen_numbers = {
    0: 'A',
    1: 'B',
    2: 'II',
    3: 'S'
}

# Code in a 'while True:' loop repeats forever
while True:
    case_true = False
    gen_num = rand.randint(0, 3)
    display.show(gen_numbers[gen_num])
    time.sleep(3.5)
    
    if gen_num == 0:
        if button_a.was_pressed:
            case_true = True
        elif any([button_b.was_pressed(), accelerometer.was_gesture('shake')]):
            case_true = False
    elif gen_num == 1:
        if button_b.was_pressed:
            case_true = True
        elif any([button_a.was_pressed(), accelerometer.was_gesture('shake')]):
            case_true = False
    elif gen_num == 2:
        if all([button_a.was_pressed, button_b.was_pressed]):
            case_true = True
        elif accelerometer.was_gesture('shake'):
            case_true = False
    elif gen_num == 3:
        if accelerometer.was_gesture('shake'):
            case_true = True
        elif any([button_b.was_pressed(), button_a.was_pressed()]):
            case_true = False
            
    if case_true:  # u dont need == true as ur var is already a bool
        display.show(Image.HAPPY)
    else:
        display.show(Image.TARGET)
        
    time.sleep(0.90)
quaint gulch
#

i dont get it since at the beginning of the while loop its case_true = false so if nothing happens it should literally just return false

young nest
#

u can check if this works

#

u had logical errors

past tartan
#

and function objects themselves are always truthy

quaint gulch
young nest
#

u want it to return false on every attempt?

#

@quaint gulch

past tartan
#

no, they want it to succeed on some actions and fail on others

quaint gulch
past tartan
#

the issue is that they are not calling those functions

quaint gulch
#

like i take too much time

past tartan
#

you must call .was_pressed() if it's a function

#

calling means adding parentheses

young nest
#
# Imports go at the top
from microbit import *
import random as rand
import time

gen_numbers = {
    0: 'A',
    1: 'B',
    2: 'II',
    3: 'S'
}

# Code in a 'while True:' loop repeats forever
while True:
    case_true = False  # Initialize case_true inside the loop
    gen_num = rand.randint(0, 3)
    display.show(gen_numbers[gen_num])
    time.sleep(3.5)
    
    if gen_num == 0:
        if button_a.was_pressed:
            case_true = True
        elif any([button_b.was_pressed(), accelerometer.was_gesture('shake')]):
            case_true = False
    elif gen_num == 1:
        if button_b.was_pressed:
            case_true = True
        elif any([button_a.was_pressed(), accelerometer.was_gesture('shake')]):
            case_true = False
    elif gen_num == 2:
        if all([button_a.was_pressed, button_b.was_pressed]):
            case_true = True
        elif accelerometer.was_gesture('shake'):
            case_true = False
    elif gen_num == 3:
        if accelerometer.was_gesture('shake'):
            case_true = True
        elif any([button_b.was_pressed(), button_a.was_pressed()]):
            case_true = False
    
    # If no actions occurred, default case_true to False
    if not (button_a.is_pressed() or button_b.is_pressed() or accelerometer.was_gesture('shake')):
        case_true = False
            
    if case_true:
        display.show(Image.HAPPY)
    else:
        display.show(Image.TARGET)
        
    time.sleep(0.90)
#

u had this part wrong

#
if not (button_a.is_pressed() or button_b.is_pressed() or accelerometer.was_gesture('shake')):
    case_true = False  
#

u made it true

#

it will be false

past tartan
#

from what I can te

young nest
#

he wants that if he doesnt do anything then it should be false

past tartan
#

they already default case_true to False at the beginning of the loop as well

young nest
#

so he takes all the possible situations and checks if the are not being done if they are not then he needs to return false

past tartan
sharp reef
#

!e

print(f'{bool( "a".isupper   ) = }')
print(f'{bool( "a".isupper() ) = }')```
untold lakeBOT
#

@sharp reef :white_check_mark: Your 3.12 eval job has completed with return code 0.

001 | bool( "a".isupper   ) = True
002 | bool( "a".isupper() ) = False
sharp reef
young nest
#

bruh

#

he went off

quaint gulch
#

im sorry i had to leave for a bit

young nest
#

alr

quaint gulch
#
# Code in a 'while True:' loop repeats forever
while True:
    case_true = False  # Initialize case_true inside the loop

but shouldnt this make it default to false anyway?

quaint gulch
#

doesnt the all make it so all of whats in there has to be true?

young nest
#

ye but when it doesnt detect anything

quaint gulch
#

or can i just have an and between them

young nest
#

it sets it to true

quaint gulch
#

so i should just have an else: setting it to false too?

sharp reef
quaint gulch
#

so both set it to false if i do nothing and if i do something wrong?

quaint gulch
#

okay i fixed it

#

it looks like this now

# Imports go at the top
from microbit import *
import random as rand
import time

gen_numbers = {
    0: 'A',
    1: 'B',
    2: 'II',
    3: 'S'
}

# Code in a 'while True:' loop repeats forever
while True:
    case_true = False  # Initialize case_true inside the loop
    gen_num = rand.randint(0, 3)
    display.show(gen_numbers[gen_num])
    time.sleep(3.5)
    
    if gen_num == 0:
        if button_a.was_pressed():
            case_true = True
        elif any([button_b.was_pressed(), accelerometer.was_gesture('shake')]):
            case_true = False
        else:
            case_true = False
    elif gen_num == 1:
        if button_b.was_pressed():
            case_true = True
        elif any([button_a.was_pressed(), accelerometer.was_gesture('shake')]):
            case_true = False
        else:
            case_true = False
    elif gen_num == 2:
        if all([button_a.was_pressed(), button_b.was_pressed()]):
            case_true = True
        elif accelerometer.was_gesture('shake'):
            case_true = False
        else:
            case_true = False
    elif gen_num == 3:
        if accelerometer.was_gesture('shake'):
            case_true = True
        elif any([button_b.was_pressed(), button_a.was_pressed()]):
            case_true = False
        else:
            case_true = False
    
    if case_true:
        display.show(Image.HAPPY)
    else:
        display.show(Image.TARGET)
        
    time.sleep(0.90)
past tartan
#

you still have not called some of those methods

quaint gulch
#

i just added
else:
case_true = False

past tartan
#

you didn't need that

#

you need to actually call the functions

quaint gulch
#

i fixed it

past tartan
#

good

#

in fact, you don't need the elifs for pressing other stuff either
they probably should be if tbf

quaint gulch
#

since if i someone does a wrong action i want it to also be false

#

and not just if they do nothing

past tartan
#

you need another if not an elif for that

quaint gulch
#

!close

untold lakeBOT
#
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.