【问题标题】:Program won't print and loop again if the answer invalid如果答案无效,程序将不会再次打印和循环
【发布时间】:2018-04-09 22:57:32
【问题描述】:

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

是我遗漏了什么还是我做错了。任何建议和帮助。谢谢

【问题讨论】:

  • 请正确缩进。由于continue不在循环内,这似乎只会给出语法错误。
  • 另外,我不知道你认为像noPeople &lt;= (int(venueList[0]['VIP Room'])) 这样的语句是做什么的,但是在不做任何结果的情况下进行这样的比较不会有任何效果。
  • 我已经编辑了问题

标签: python python-3.x


【解决方案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 头部中的条件更优雅。

【讨论】:

    【解决方案2】:

    它接缝你的意思不是

    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
    

    它工作得更快!

    【讨论】:

      猜你喜欢
      • 2021-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-13
      • 2014-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多