如果答案无效,程序将不会打印并再次循环

时间:2017-10-28 13:27:53

标签: python python-3.x

我有这个程序,我想检查所选地点是否可以容纳人数(noPeople)。 返回FALSE如果无效,如果无法容纳其他人,则返回TRUE(如果有效)。

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

似乎我的程序不想循环,即使房间无法容纳足够的人,也不会打印无效的场地。

[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
>>> 

我错过了什么,或者我做错了。任何建议和帮助。感谢

2 个答案:

答案 0 :(得分:1)

你有一些小错误。缺少if和错误的最后一个案例。

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:'))

另外,虽然您可以使用while truebreak,但我认为检查while头部的情况会更优雅。

答案 1 :(得分:1)

它接缝你的意思不是

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

但是这个:

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

比所有作品都要好)

但是!如果你想用类似Python的方式编写代码,那么这样做会更好:

    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

效果更快!!