0

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

Heggelund
  • 5
  • 1

4 Answers4

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.

lksj
  • 49
  • 7
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")
        
wombat
  • 614
  • 3
  • 18
  • Yeah thanks, noticed fast that it was a hot mess, have to take it 1 thing at a time to just get my head in on the job ;) thanks alot! – Heggelund Nov 26 '21 at 04:15
0

Your code has a bunch of issues, here those have been fixed:

more_guests = 0
# you don't need to declare types, but if you want to, this is how
loop: bool = False
# however, Python can just infer the type itself like this
ferdig = 1
# you don't want to reset guests every time around the loop
guests = []
while loop == False:
    more_guests = 0
    if int(ferdig) == 1:
        guest = input("type in guest ")
        # you always want to append a guest, including if someone types a 1 after
        guests.append(guest)
        more_guests = int(input("done? 1 for yes 2 for no "))
        if int(more_guests) == 1:
            ferdig = 3
        elif int(more_guests) == 2:
            ferdig = 2
        else:
            print("invalid answer, please use 1 or 2")
            ferdig = 1
    elif int(ferdig) == 2:
        ferdig = 1
    elif int(ferdig) == 3:
        # after printing, you're done, you don't want to print forever
        print(guests)
        loop = True

Note that most of your code isn't really needed though, you're doing a lot of book-keeping that Python can do for you, or that's just not needed:

# this isn't needed, because you set that at the start of the loop anyway
# more_guests = 0
# this isn't needed, because you can tell when to stop from ferdig
# loop: bool = False
# starting at 2, since that means you want to keep going
ferdig = 2
guests = []
while ferdig != 1:
    # this isn't needed, you can just read ferdig
    # more_guests = 0
    # this isn't needed, you want a new guest on every loop
    #if int(ferdig) == 1:
    guest = input("type in guest ")
    guests.append(guest)
    ferdig = int(input("done? 1 for yes 2 for no "))
    # none of this is needed, all you need to know is if ferdig is 1 or 2
    # if int(more_guests) == 1:
    #     ferdig = 3
    # elif int(more_guests) == 2:
    #     ferdig = 2
    # else:
    if ferdig not in (1, 2):
        print("invalid answer, please use 1 or 2")
        ferdig = 1
    # this is also not needed, at this point ferdig will be 1 or 2
    # elif int(ferdig) == 2:
    #     ferdig = 1
    # elif int(ferdig) == 3:
# put the print outside the loop and it only prints once
print(guests)
# got rid of this
# loop = True

So, that's just:

ferdig = 2
guests = []
while ferdig != 1:
    guest = input("type in guest ")
    guests.append(guest)
    ferdig = int(input("done? 1 for yes 2 for no "))
    if ferdig not in (1, 2):
        print("invalid answer, please use 1 or 2")
        ferdig = 1
print(guests)

In the end, this would do the same:

more_guests = True
guests = []
while more_guests:
    guests.append(input("type in guest "))
    more_guests = input("done? 1 for yes") != '1'
print(guests)
Grismar
  • 27,561
  • 4
  • 31
  • 54
  • Thank you, noticed that fast when i continued, but have cleaned mine up so it works, altough not as great as here! Just trying my way forward since im not that familiar with python yet and it has been a while since ive programmed at all, but thanks alot for this great example and explanation! – Heggelund Nov 26 '21 at 04:00
  • i am curious about this line though `more_guests = input("done? 1 for yes") != '1'` how come it stops when you type 1 when the loop runs on more_guests = True havent seen it used before so just struggling a bitr – Heggelund Nov 26 '21 at 04:09
  • That's alright, that *is* a bit dense. `input("done? 1 for yes")` just gets the input, as a string. `x != '1'` is an expression that's `True` if `x` is not `'1'` and `False` otherwise - `!=` means "not equal to". So, `more_guests = input("done? 1 for yes") != '1'` means `more_guests` should be `True` if some text entered by the user on that prompt is not `'1'`, which is exactly what you need: enter a '1', `more_guests` will be `False`, otherwise it will be `True`. – Grismar Nov 26 '21 at 04:15
  • I suppose what can be very confusing is something like this in general: `x = y == 2` - the single equals sign is an assignment, so Python reads this as `x = (y == 2)` in a sense. And the double equals sign is a comparison, which will result in a boolean value of `True` or `False`, depending on whether both sides have the same value (or however equality is defined for the type being compared). So it works out to `x = True` or `x = False`. – Grismar Nov 26 '21 at 04:18
  • yeah ok i knew them for themselves just didnt see the whole picture when put together, so if i understand correctly it works out so that when you type 1 it becomes `more_guests = (1 != 1)` which turnes to `more_guests = (False)` Thanks a bunch that actually helped alot! – Heggelund Nov 26 '21 at 04:32
  • Happy to help. Keep asking good questions, with clear examples, to get the most out of SO. – Grismar Nov 26 '21 at 04:34
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()
  • interesting, what are the benefits of using functions for it over a loop? – Heggelund Nov 26 '21 at 04:36
  • Personally I try to avoid While loops because they can result in infinite loops if you aren't careful. I also usually find functions easier to read and wrap my head around. But it really comes down to you and what you prefer. –  Nov 26 '21 at 04:44
  • As the duplicate post says: don't underestimate the ingenuity of fools; using functions can lead to an infinite recursion error. – Martijn Pieters May 15 '22 at 16:04