Python variable ändern (erhöhen oder verringern)?

Ich bin ein absoluter Anfänger was programmieren angeht, lerne aber immer mehr und mehr dazu. Ich möchte für etwas ein Python Script erstellen, was auf meinem Pi die ganze Zeit läuft und die MQTT Nachrichten mitliest, um diese auszuführen und/oder eine Nachricht zurück zu senden.

Ich habe versucht eine Nachricht zu senden der den wert der Variable um 1 erhöhen soll und damit auch die While schleife aktivieren soll. Jedoch funktioniert es nicht, da die variable nicht in "def on_message" verfügbar ist und es die Erhöhung quasi nicht nach außen austrägt.

Was gibt es für Möglichkeiten die variable zu erhöhen, sodass sich die while schleife aktiviert? Und gibt es auch andere Ansätze wie man eine diese Schleife machen kann?

Script:

import paho.mqtt.client as mqtt
import os
import subprocess
import time
import smbus2
import bme280

#Bme280_basic Temperature
# BME280 sensor address (default address)
address = 0x76
# Initialize I2C bus
bus = smbus2.SMBus(1)
# Load calibration parameters
calibration_params = bme280.load_calibration_params(bus, address)
# to activate loop
y = int(1)
#Temperature loop
while y == 2:
      data = bme280.sample(bus, address, calibration_params)

      # Extract temperature, pressure, and humidity
      temperature_celsius = data.temperature      
      # Print the readings
      print("Temperature: {:.2f} °C".format(temperature_celsius))
      # Wait for a few seconds before the next reading
      time.sleep(2)
      #print(y)
else:
      print("stopped") 
    
#Connection successfull
def on_connect(client, userdata, flags, rc):
  print("Connected to MQTT broker")


#Checking for messages to execute code
def on_message(client, userdata, msg):
    print(msg.topic+" "+str(msg.qos)+" "+str(msg.payload))
    if msg.payload.decode() == "temp_bme280_start":
        print("Calling script to for temperature start...")
        # activate loop by adding 1
        y += 1   
        print(y)
    if msg.payload.decode() == "temp_bme280_stop":
        print("Calling script to for temperature stop...")
        # deactivate loop by remove 1
        y -= 1  
        print(y)  

def on_subscribe(client, userdata, mid, granted_qos):
    print("Subscribed to topic : " + str(mid) +" with QoS" + str(granted_qos))

client = mqtt.Client()

client.username_pw_set( "userxxx" , "passwortxxx" )

client.connect( "192.16x.xxx.xxx", 1883, 60)

client.subscribe( "frame/monitor" , qos=1)

client.on_connect = on_connect

client.on_message = on_message

client.loop_forever()

Würde Chat GPT sowas lösen können?

Code, Programmiersprache, Python, Script, Python 3, Raspberry Pi, while-Schleife, ChatGPT
Java While-Schleife hört nicht auf?

Hallo. Ich versuche auf Java ein Spiel zu programmieren und meine Schleife hört einfach nicht auf zu laufen also das Spiel hängt sich quasi auf und macht nichts mehr, ihr wisst was ich meine :D
Wäre super lieb wenn mir jemand helfen könnte.

Hier ist mein Code:

boolean nochmal;

   nochmal = true;

   if (läuft) {

     while (nochmal) {

       if (tf_frage.getText().equals("14*6") && tf_eingabe.getText().equals("84")) {

         tf_eingabe.setBackground(Color.green);

         nochmal = false;

         JOptionPane.showMessageDialog(null, "Ergebnis", "Ihre Antwort war richtig!", JOptionPane.INFORMATION_MESSAGE);

       } // end of if

       else if (tf_frage.getText().equals("13*8") && tf_eingabe.getText().equals("104")){

         tf_eingabe.setBackground(Color.green);

         nochmal = false;

         JOptionPane.showMessageDialog(null, "Ergebnis", "Ihre Antwort war richtig!", JOptionPane.INFORMATION_MESSAGE);

       } // end of if-else

       else if(tf_frage.getText().equals("11*12") && tf_eingabe.getText().equals("132")) {

         tf_eingabe.setBackground(Color.green);

         nochmal = false;

         JOptionPane.showMessageDialog(null, "Ergebnis", "Ihre Antwort war richtig!", JOptionPane.INFORMATION_MESSAGE);

       } // end of if-else

       else if (tf_frage.getText().equals("19*4") && tf_eingabe.getText().equals("76")) {

         tf_eingabe.setBackground(Color.green);

         nochmal = false;

         JOptionPane.showMessageDialog(null, "Ergebnis", "Ihre Antwort war richtig!", JOptionPane.INFORMATION_MESSAGE);

       } // end of if-else

       else if (tf_frage.getText().equals("9*16") && tf_eingabe.getText().equals("144")){

         tf_eingabe.setBackground(Color.green);

         nochmal = false;

         JOptionPane.showMessageDialog(null, "Ergebnis", "Ihre Antwort war richtig!", JOptionPane.INFORMATION_MESSAGE);

       } // end of if-else

       else if (tf_frage.getText().equals("12*17") && tf_eingabe.getText().equals("204")) {

         tf_eingabe.setBackground(Color.green);

         nochmal = false;

         JOptionPane.showMessageDialog(null, "Ergebnis", "Ihre Antwort war richtig!", JOptionPane.INFORMATION_MESSAGE);

       } // end of if-else

       else if (tf_frage.getText().equals("16*7") && tf_eingabe.getText().equals("112")) {

         tf_eingabe.setBackground(Color.green);

         nochmal = false;

         JOptionPane.showMessageDialog(null, "Ergebnis", "Ihre Antwort war richtig!", JOptionPane.INFORMATION_MESSAGE);

       } // end of if-else

       else {

         tf_eingabe.setBackground(Color.red);

       } // end of if-else

     } // end of while

   } // end of if

Computer, Java, while-Schleife

Meistgelesene Fragen zum Thema While-Schleife