亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

僅當一個或多個項目時,我如何使 for 循環工作!='是'

僅當一個或多個項目時,我如何使 for 循環工作!='是'

烙印99 2022-05-24 12:52:14
我正在做一個硬件任務,在那里我創建一個帶有入門問題的假俱樂部。如果任何問題的回答為“否”,則不允許此人加入。我試過回到關于列表和循環的 og 課程,但我找不到我想要在那里做的事情。到目前為止,這是我的代碼。# Purpose: Create a fake club that has certain requirements, ask user to# fill out the application, and print out their answers + results.def main():    display = input('Hi! This is your application to The Aqua Project...')    display2 = input('Read the following questions and just type y or n')# Weird list format...    user = [input('Are you at least 18 yrs old? '),    input('Can you work with other people? '),    input('Do you like animals? '),    input('Are you okay with getting dirty sometimes? ')]# Here's the problem, I want to print 'sorry you cant join' once...    for i in range(4):        if user[i] != 'y':            print('Sorry, but you can\'t join our club')            justToShowInCMD = input('')            i += 1        else:            print('')            print('Congratulations, you have met all of our requirements!')            print('We will send an email soon to discuss when our team')            print('will meet up to help save some animals!')            print('In the meantime, visit our website at             TheAquaProject.com')            justToShowInCMD = input('')main()當您為某些問題添加“n”時,它表示您可以加入,但對于其他問題,它表示您不能加入。我不知道為什么有時當你在面試中拒絕時它說你可以,但不應該。
查看完整描述

3 回答

?
Helenr

TA貢獻1780條經驗 獲得超4個贊

執行此操作的常用方法是帶有 a和子句的for循環:breakelse


for answer in user:

    if answer != 'y':

        print('Sorry')

        break

else:

    print('Congratulations')

或any()功能:


if any(answer != 'y' for answer in user):

    print('Sorry')

else:

    print('Congratulations')


查看完整回答
反對 回復 2022-05-24
?
江戶川亂折騰

TA貢獻1851條經驗 獲得超5個贊

假設您range(4)基于 的長度迭代 4 次(使用 )user,您可以簡單地執行以下操作:


           if 'n' or 'no' in user:

               print('Sorry, but you can\'t join our club')

               justToShowInCMD = input('')

           else:

           print('')

           print('Congratulations, you have met all of our requirements!')

           print('We will send an email soon to discuss when our team')

           print('will meet up to help save some animals!')

           print('In the meantime, visit our website at 

           TheAquaProject.com')

           justToShowInCMD = input('')


您可以修改if條件以適應其他形式的否定答案,例如'N' or 'No'. 您不需要遍歷user.


查看完整回答
反對 回復 2022-05-24
?
慕姐4208626

TA貢獻1852條經驗 獲得超7個贊

如果一個“否”表示拒絕,您可以break在打印拒絕信息后添加退出循環。就像:


for i in range(4):

        if user[i] != 'y':

            print('Sorry, but you can\'t join our club')

            justToShowInCMD = input('')

            # i += 1   # <<<<<<<<<<<<<<<<<< this code may be not needed here

            break      # <<<<<<<<<<<<<<<<<< where to add break

        else:

            print('')

            print('Congratulations, you have met all of our requirements!')

            print('We will send an email soon to discuss when our team')

            print('will meet up to help save some animals!')

            print('In the meantime, visit our website at 

            TheAquaProject.com')

            justToShowInCMD = input('')

或者你可以使用一個變量來表示是否拒絕,就像:


toDecline = False

for i in range(4):

        if user[i] != 'y':

            toDecline = True


if toDecline:

    print('Sorry, but you can\'t join our club')

    justToShowInCMD = input('')

else:

    print('')

    print('Congratulations, you have met all of our requirements!')

    print('We will send an email soon to discuss when our team')

    print('will meet up to help save some animals!')

    print('In the meantime, visit our website at 

    TheAquaProject.com')

    justToShowInCMD = input('')


查看完整回答
反對 回復 2022-05-24
  • 3 回答
  • 0 關注
  • 114 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號