Joe han
Joe han

Reputation: 171

Program won't print and loop again if the answer invalid

I have this program where I want to check if the selected venue can accommodate to the number of people (noPeople). Return FALSE if invalid where it cannot accommodate the number of people else return a TRUE if valid.

print('''
    [1] vip room(10 person)            [4]Banquet Hall(200 person)
    [2] executive room(30 person)      [5]Chamber Hall(500 person)
    [3] pool site(50 person)           [6]Concert Hall(1000 person)
    ''')
def validateVenue(choice,venueList,noPeople):

    if choice == '1':
        noPeople <= (int(venueList[0]['VIP Room']))
        return True

    elif choice == '2':
        noPeople <= (int(venueList[0]['Executive Room']))
        return True

    elif choice == '3':
        noPeople <= (int(venueList[0]['Pool Site']))
        return True

    elif choice == '4':
        noPeople <= (int(venueList[0]['Banquet Hall']))
        return True

    elif choice == '5':
        noPeople <= (int(venueList[0]['Chamber Hall']))
        return True

    elif choice == '6':
        noPeople <= (int(venueList[0]['Concert Hall']))
        return True

    else:
        print('Invalid venue, please choose again.')
        return False

while True:
    noPeople = int(input('people:'))
    venueList = [{'VIP Room':10,'Executive Room':30,'Pool Site':50,'Banquet Hall':200,'Chamber Hall':500,'Concert Hall':1000}]
    choice = input('Please select a venue:')

    if validateVenue(choice,venueList,noPeople):
        break

And it seems that my program doesn't want to loop and it won't print the invalid venue even though the room can't accommodate enough people.

[1] vip room(10 person)            [4]Banquet Hall(200 person)
[2] executive room(30 person)      [5]Chamber Hall(500 person)
[3] pool site(50 person)           [6]Concert Hall(1000 person)

people:50
Please select a venue:1
>>> 

Am I missing something or I am doing it wrong. Any suggestion and helps. Thanks

Upvotes: 1

Views: 66

Answers (2)

Vasyl Kolomiets
Vasyl Kolomiets

Reputation: 441

it seams You mean not

if choice == '1':
    noPeople <= (int(venueList[0]['VIP Room']))
    return True

but this:

if choice == '1' and noPeople <= (int(venueList[0]['VIP Room']))
    return True

than all works )

But! If you want to write code in Python-like style it will be better this way:

    print('''
    [1] vip room(10 person)            [4]Banquet Hall(200 person)
    [2] executive room(30 person)      [5]Chamber Hall(500 person)
    [3] pool site(50 person)           [6]Concert Hall(1000 person)
    ''')
work = {'1':'VIP Room','2':'Executive Room','3':'Pool Site',
        '4':'Banquet Hall','5':'Chamber Hall','6':'Concert Hall'}

def validateVenue(choice,venueList,noPeople):
    if choice in work.keys():
        if noPeople <= (int(venueList[0][work[choice]]):
            return True
        else:
            return False
    else:
        print('Invalid venue, please choose again.')
        return False

venueList = [{'VIP Room':10,'Executive Room':30,'Pool Site':50,'Banquet Hall':200,'Chamber Hall':500,'Concert Hall':1000}]
while True:
    noPeople = int(input('people:'))

    choice = input('Please select a venue:')
    if validateVenue(choice,venueList,noPeople):
        break

It works much faster!!

Upvotes: 1

voiDnyx
voiDnyx

Reputation: 1067

You had some minor mistakes. Missing if and a wrong last case.

print('''
    [1] vip room(10 person)            [4]Banquet Hall(200 person)
    [2] executive room(30 person)      [5]Chamber Hall(500 person)
    [3] pool site(50 person)           [6]Concert Hall(1000 person)
    ''')
def validateVenue(choice,venueList,noPeople):

    if choice == '1':
        if noPeople <= (int(venueList[0]['VIP Room'])):
            return True

    elif choice == '2':
        if noPeople <= (int(venueList[0]['Executive Room'])):
            return True

    elif choice == '3':
        if noPeople <= (int(venueList[0]['Pool Site'])):
            return True

    elif choice == '4':
        if noPeople <= (int(venueList[0]['Banquet Hall'])):
            return True

    elif choice == '5':
        if noPeople <= (int(venueList[0]['Chamber Hall'])):
            return True

    elif choice == '6':
        if noPeople <= (int(venueList[0]['Concert Hall'])):
            return True

    print('Invalid venue, please choose again.')
    return False

venueList = [{'VIP Room':10,'Executive Room':30,'Pool Site':50,'Banquet Hall':200,'Chamber Hall':500,'Concert Hall':1000}]

choice = input('Please select a venue:')
noPeople = int(input('people:'))

while not validateVenue(choice,venueList,noPeople):
    choice = input('Please select a venue:')
    noPeople = int(input('people:'))

Also while you can use while true and break i think it more elegant to check the condition in the while head.

Upvotes: 1

Related Questions