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

标签 python python-3.x

我有一个程序,我想检查所选 field 是否可以容纳人数(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

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

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

我是否遗漏了什么或者我做错了什么?任何建议和帮助。谢谢

最佳答案

你犯了一些小错误。缺少 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 头中的条件更优雅。

关于python - 如果答案无效,程序将不会打印并再次循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46990244/

相关文章:

python - 随机技能在 Python 文本角色扮演游戏中无法按预期发挥作用

python - 如何在python中转储http请求

python - 返回超出所有先前元素的元素列表

python - 找到总和为三个最小数字的排列

python - 父类(super class)中的调用方法没有给出我期望的输出?

python - 使用 python 下载部分 youtube 视频

python - dbt 高级用户 'NoneType' 没有属性 'find_generate_macro_by_name'

python - 将 xlsx 转换为 parquet

python-3.x - 如何让TQDM进度条在KB和MB之间自动更新

python - 根据共同值将列表合并在一起