第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

如果沒有一個if是真的,我如何打印else?

如果沒有一個if是真的,我如何打印else?

qq_花開花謝_0 2022-05-24 10:38:38
我正在嘗試打印您有資格獲得的東西,如果您沒有資格獲得其中任何一項,我正在嘗試打印您沒有資格獲得其中任何一項。但是使用此代碼,它總是將 else 與 if 一起打印PS:我1個月前開始編程。print('Task 2')print()name = str(input('Navn: '))age = int(input('Age: '))gender = str(input('Gender (f for female, m for male): '))children = int(input('How many children: '))repute = int(input('repute, on a scale from 1(bad) to 9(immaculate): '))record = str(input('record (done time/convicted/suspect/clean): '))seniority = int(input('seniority (years): '))print()member = gender == 'f' and age >= 20 and repute <= 7solder = gender == 'f' and age >= 20 and age <= 30 and repute <= 7 and children == 0sargent = gender == 'f' and age >= 20 and age <= 30 and repute <= 7 and children == 0 and seniority >= 4 and record != 'clean'captain = gender == 'f' and age >= 20 and age <= 40 and repute <= 7 and seniority >= 6 and record == ('done time' or 'convicted')commander = gender == 'f' and age >= 20 and age <= 40 and repute <= 3 and seniority >= 6 and record == ('done time' or 'convicted')president = gender == 'f' and age >= 20 and age <= 40 and repute <= 2 and seniority >= 8 and record == ('done time' or 'convicted') and name != 'kim'print('You are eligible to become:')if member == True:    print('Member')if solder == True:    print('Solder')if sargent == True:    print('Sargent')if captain == True:    print('Captain')if commander == True:    print('Commander')if president == True:    print('President')else:     print('You are not eligible to be a member')
查看完整描述

3 回答

?
暮色呼如

TA貢獻(xiàn)1853條經(jīng)驗 獲得超9個贊

使用最終if條件來檢查是否沒有任何條件匹配,而不是else:


if member == True:

    print('Member')

if solder == True:

    print('Solder')

if sargent == True:

    print('Sargent')

if captain == True:

    print('Captain')

if commander == True:

    print('Commander')

if president == True:

    print('President')

if not (member or solder or sargent or captain or commander or president): 

    print('You are not eligible to be a member')

編輯:修改為打印所有符合條件的職位。


查看完整回答
反對 回復(fù) 2022-05-24
?
互換的青春

TA貢獻(xiàn)1797條經(jīng)驗 獲得超6個贊

你可以替換


else: 

    print('You are not eligible to be a member')


if all([not cond for cond in [member, soldier, sargent, captain, commander, president]]):

    print('You are not eligible to be a member')

甚至按照評論的建議,使用生成器表達(dá)式,以獲得理論上更好的性能


if all(not cond for cond in [member, soldier, sargent, captain, commander, president]):

    print('You are not eligible to be a member')


查看完整回答
反對 回復(fù) 2022-05-24
?
aluckdog

TA貢獻(xiàn)1847條經(jīng)驗 獲得超7個贊

只是多了一個選項,因此,如果您必須添加更多可能性,您只需將它們插入中間,而不必更改任何其他語句。


msg = []

if member:

  msg.append('Member')

if solder:

  msg.append('Soldier')

..

..

if new_possibility:

   msg.append('blah')


if msg:

   print print '\n'.join(msg)

else:

   print 'Not elligible'


查看完整回答
反對 回復(fù) 2022-05-24
  • 3 回答
  • 0 關(guān)注
  • 185 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號