Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

216
Visualizações
if statement, going back and forth

i am new to python, but have programmed some structured text, and a tiny bit c++ now i am working on a problem using if statements and it doesnt seem to work the way i am used to

example code:

more_guests = 0
loop = bool; loop = False
ferdig = int; ferdig = 1
while loop == False:
    guests = []
    more_guests = 0
    if int(ferdig) == 1:
        guest = input("type in guest ")
        more_guests = int(input("done? 1 for yes 2 for no "))
        if int(more_guests) == 1:
          guests.append(guest)
          ferdig == 3
        elif int(more_guests) == 2:
            guests.append(guest)
            ferdig == 2
        else:
            print("unvalid answer, please use 1 or 2")
            ferdig == 1
    elif int(ferdig) == 2:
        ferdig = 1
    elif int(ferdig) == 3:
        print(guests)
    else:
        loop = True

ive tried making sure its an integer and so on, just keep getting stuck on done? 1 for yes 2 for no, it always loop me back to type in guest

while using structured text i often used this way of going back and forth, but i cant seem to understand it in python, maybe i should rather use case/switch? anyways if anyone could help me understand if you can use the IF statement this way in python is highly appreciated

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Here is another option to accomplish what you are trying to do using functions.

guests = []

def add_guest():

    guest = input("type in guest ")
    guests.append(guest)
    add_more()

def add_more():

    more_guests = int(input("done? 1 for yes 2 for no ")) 

    #if they answer 1 we print out the final list
    if more_guests == 1: 
        print("the guest list is {}".format(guests))
        print('have a nice day')  

    #if they answer 2 we go to the add_guest function    
    elif more_guests == 2:  
        add_guest()

    #if they answer anything other than 1 or 2 we re-call the add_more function.  
    else: 
        print("unvalid answer, please use 1 or 2") 
        add_more()

add_guest()
over 4 years ago · Santiago Trujillo Relatório

0

I think you wrote wrong operator. ferdig == 1 just comparing value operator. So it's return true(1) or false(0).

if you wnat to change the ferdig's value you should write ferdig = 3 in if statement.

over 4 years ago · Santiago Trujillo Relatório

0

I've cleaned up your code a bit to get what I think you want.

guests = []
while True:
    guest = input("type in guest ")
    guests.append(guest)
    response = int(input("done? 1 for yes 2 for no "))
    if response == 1:
        print(guests)
        break
    elif response == 2:
        continue
    elif response == 3:
        print(guests)
    else:
        print("invalid answer, please use 1 or 2")
        
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda