Jump to content
Nepenthe

Help! Nu pot opri o functie din a rula la nesfarsit(Python)

Recommended Posts

Posted

Salut! Am un mic blocaj in demersul meu de a invata python. Fiind vorba de functii (banale pentru unii) am in poza atasata o poza a ecranului. Cand incerc sa tastez 'a' de exemplu,  merge la nesfarsit; cum as putea sa opresc asta si sa afiseze doar o data? am incercat eu cate ceva dar ori mi-a dat eroare, ori mergea la fel. 

Asta e poza: https://ibb.co/SXfqFB7

P.S. : ignorati textul; e un fel de inside joke :))

Posted (edited)

In functia menu() tu iei inputul de la user in user_input dupa care te bagi direct in while loop.

Valoarea user_input nu se poate schimba niciodata pt ca tu esti blocat in while:

 

def menu():
    user_input='fara valoare' # intrii in loop cu o valoare default
    
    while user_input != 'q':
        user_input = input("*inside joke*: ") # inputul in citesti in interiorul loopului
        if user_input == 'a':                # cand while se executa a 2-a oara promptul de input se executa din nou
            print("ai tastat a")
        elif user_input == 'b':
            print("ai tastat b")
        else:
            print("nu stii sa tastezi?")
    #nu mai are rost aici sa mai citesti input

Sper sa intelegi tu cum functioneaza while-ul. Daca inca nu te prinzi incearca un step-by-step debugging. (http://pythontutor.com/)

Edited by yoyois
  • Thanks 2

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...