0

Hello Stackoverflow Community,

I'm trying to write a Program which do simple Tasks. The main Goal is to set Variables with Numbers to press and then it should repeat.

The Problem i have is that i cant get out the Loop. The second thing is that i only want to repeat the Question which is over 6 not every Question

import os #Für Shutdown
import time #Für Access auf Zeit
import pyautogui #Zugang zu Keyboard & Mouse Funktionen
while True:

    Zeit = int(input("Gebe eine Sekundenzahl ein bis man Essen & Trinken soll: "))
    if Zeit > 3200:
        print("Deine Taschen werden überfüllt sein, wähle weniger Sekunden");
        continue
    else:
        while True:   
            Trinken = int(input("In welcher Tasche hast du dein Trinken: "))
            if Trinken > 6:
                print("Es gibt keinen Slot nach 6")
                continue
            else:
                Essen = int(input("In welcher Tasche hast du dein Essen: "));
                if Essen > 6:
                    print("Es gibt keinen Slot nach 6")
                    continue

os.system("shutdown /s /t 19200");

while True:
    time.sleep (15); # Zeit um IC zu gehen
    pyautogui.press(Trinken)
    time.sleep(20) # Zeitfenster für Trinkvorgang
    pyautogui.press(Essen) 

    time.sleep(Zeit) #Zeit zwischen den jeweiligen Ess und Trinkvorgängen
    pyautogui.press(Trinken)
    time.sleep(20) # Zeitfenster für Trinkvorgang
    pyautogui.press(Essen) 

What am i doing wrong? The program isnt entering "part2"

Thanks for every help!

IIIIII
  • 3
  • 3

3 Answers3

0

It looks like on line 4 you start a while loop but fail to indent, this could cause an indentation issue. I also am not sure where part 2 is within your code, I notice you are trying to use os.shutdown, I would recommend commenting that out until you are done or debugging this script could get difficult with your computer turning off everytime you run the code. After changing these elements the code looks like so:

import os #Für Shutdown
import time #Für Access auf Zeit
import pyautogui #Zugang zu Keyboard & Mouse Funktionen
while True:

    Zeit = int(input("Gebe eine Sekundenzahl ein bis man Essen & Trinken soll: "))
    if Zeit > 3200:
        print("Deine Taschen werden überfüllt sein, wähle weniger Sekunden");
        continue
    else:
        while True:   
                Trinken = int(input("In welcher Tasche hast du dein Trinken: "))
                if Trinken > 6:
                    print("Es gibt keinen Slot nach 6")
                    continue
                else:
                    Essen = int(input("In welcher Tasche hast du dein Essen: "));
                    if Essen > 6:
                        print("Es gibt keinen Slot nach 6")
                        continue

#os.system("shutdown /s /t 19200");

while True:
    time.sleep (15); # Zeit um IC zu gehen
    pyautogui.press(Trinken)
    time.sleep(20) # Zeitfenster für Trinkvorgang
    pyautogui.press(Essen) 

    time.sleep(Zeit) #Zeit zwischen den jeweiligen Ess und Trinkvorgängen
    pyautogui.press(Trinken)
    time.sleep(20) # Zeitfenster für Trinkvorgang
    pyautogui.press(Essen) 

I believe you are referring to the fact it is stuck looping the same 2 questions. Iw ould recommend not using while True for every branch of your code as often times this is not the condition you really want to test nor do you actually need these conditionals to have your code execute how you think it will. Use something like this:

import os #Für Shutdown
import time #Für Access auf Zeit
import pyautogui #Zugang zu Keyboard & Mouse Funktionen

Zeit = int(input("Gebe eine Sekundenzahl ein bis man Essen & Trinken soll: "))
if Zeit > 3200:
    print("Deine Taschen werden überfüllt sein, wähle weniger Sekunden");
else:
    Trinken = int(input("In welcher Tasche hast du dein Trinken: "))
    if Trinken > 6:
        print("Es gibt keinen Slot nach 6")
    else:
        Essen = int(input("In welcher Tasche hast du dein Essen: "));
        if Essen > 6:
            print("Es gibt keinen Slot nach 6")

#os.system("shutdown /s /t 19200");

time.sleep (1); # Zeit um IC zu gehen
pyautogui.press(Trinken)
time.sleep(2) # Zeitfenster für Trinkvorgang
pyautogui.press(Essen) 

time.sleep(Zeit) #Zeit zwischen den jeweiligen Ess und Trinkvorgängen
pyautogui.press(Trinken)
time.sleep(2) # Zeitfenster für Trinkvorgang
pyautogui.press(Essen) 

This will be easier to trace errors and debug. I must confess I do not speak german and simply am helping with how I would approach debugging this sort of program.

0

Looking through your code, it seems like you never change the condition from True to False, so it will always be infinite. According to this post, while True is essentially synonymous with looping forever, and you never break from the loop, thus it never ends.

My personal approach would be assigning the boolean value to a variable such as 'repeating'.

repeating = True

This makes it easier to read, and to follow the logic of your program. It's also more pythonic. Then structure the code as you have but instead of just while True use:

while repeating is True:
//do stuff

Then within the loop, create a condition that will cause isRepeating to change to False.

if (x < 6):
repeating = False

Then, you will break out of your loop.

Dharman
  • 30,962
  • 25
  • 85
  • 135
coderustle
  • 11
  • 2
  • thanks a lot i used now this approach. but got another problem :) will write a answer to my question – IIIIII Mar 18 '22 at 20:07
0

We fixxed together the first problem know the "Menu" is working fine.

The problem is now that the second part dont execute. (Second Part begins from the last while True:)

import os #Für Shutdown
import time #Für Access auf Zeit
import pyautogui #Zugang zu Keyboard & Mouse Funktionen

repeat = True


while repeat is True:

    Zeit = int(input("Gebe eine Sekundenzahl ein bis man Essen & Trinken soll: "))
    if Zeit > 3200:
        print("Deine Taschen werden überfüllt sein, wähle weniger Sekunden");
        continue
    else:
        while repeat is True:   
                Trinken = int(input("In welcher Tasche hast du dein Trinken: "))
                if Trinken > 6:
                    print("Es gibt keinen Slot nach 6")
                    continue
                else:
                    Essen = int(input("In welcher Tasche hast du dein Essen: "));
                    if Essen > 6:
                        print("Es gibt keinen Slot nach 6")
                        continue
                    else:
                        repeat = False
                        continue

os.system("shutdown /s /t 19200");

**while True:
    time.sleep (15); # Zeit um IC zu gehen
    pyautogui.press(Trinken)
    time.sleep(20) # Zeitfenster für Trinkvorgang
    pyautogui.press(Essen) 


    time.sleep(Zeit) #Zeit zwischen den jeweiligen Ess und Trinkvorgängen
    pyautogui.press(Trinken)
    time.sleep(20) # Zeitfenster für Trinkvorgang
    pyautogui.press(Essen)**

I marked the Part which isnt working bold. It looks like it crashes when it try to press a key

Thanks for every help guys

IIIIII
  • 3
  • 3