【问题标题】:STACK OVERFLOW IS TRASH ????sdsadsadasdsadasdsad [duplicate]堆栈溢出是垃圾????sdsadsadasdsadasdsad [重复]
【发布时间】:2022-07-28 07:21:56
【问题描述】:

堆栈溢出是垃圾????sdsadsadasdsadasdsadSTACK 溢出是垃圾????sdsadsadasdsadasdsadSTACK 溢出是垃圾????sdsadsadasdsadasdsadSTACK 溢出是垃圾????sdsadsadasdsadasdsadSTACK 溢出是垃圾????sdsadsadasdsadasdsadSTACK 溢出是垃圾????sdsadsadSTACK 溢出?sdsadsadasdsadasdsadSTACK 溢出是垃圾 ????sdsadsadasdsadasdsadSTACK 溢出是垃圾 ????sdsadsadasdsadasdsadSTACK 溢出是垃圾 ????sdsadsadasdsadasdsadSTACK 溢出是垃圾 ????sdsadsadasdsadasdsad

【问题讨论】:

    标签: python


    【解决方案1】:

    while True: 循环中提示输入并在name in options 时中断:

    options = {'Dan': 1, 'Bianca': 2, 'Bob': 3}
    while True:
        name = input('Enter your name: ')
        if name in options:
            break
        print(f'{name} was NOT found')
        print(False)
    
    print(f'{name} was found in the list')
    print(True)
    

    【讨论】:

      【解决方案2】:

      如果你想在列表中有匹配的名字的情况下停止循环,你可以这样做

      def check_names(name, options):
          print('Searching for ' + name + ' in the list...')
      
          if name in options:
              print(name + ' was found on the list')
              found = True
              return found
          else:
              print(name + ' was NOT found')
              found = False
              return found
      
      found = False
      while found == False:
          name = input('Enter your name: ')   
          options = {'Dan': 1, 'Bianca': 2, 'Bob': 3}
          found = check_names(name, options)
          print(found)
      

      【讨论】:

        【解决方案3】:
        options = {'Dan': 1, 'Bianca': 2, 'Bob': 3}
        
        while True:
            name = input('Enter your name: ')   
        
            key = name
        
            print('Searching for ' + name + ' in the list...')
        
            if key in options:
                print(name + ' was found on the list')
                found = True
                print(found)
                break  # <-- Breaks out of the while loop if name was found.
            else:
                print(name + ' was NOT found')
                found = False
                print(found)
        

        【讨论】: