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

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

我如何使用邏輯運算符

我如何使用邏輯運算符

斯蒂芬大帝 2021-08-17 10:13:06
我已經嘗試了所有方法,如果您不選擇“ST”,它會不斷地在 while 循環中循環。我不知道該怎么做,如果有人能告訴我,那將非常有幫助。我在頂部添加了一些上下文的代碼;我只需要有關 while 循環的幫助。我正在使用while循環,所以如果他們沒有選擇給定的位置,他們必須重新選擇。這是我的代碼:pos = input("What Is Your Choice")if pos == "ST":    shot = 8    print("Shot Is",shot)    passing = 6    print("Passing Is",passing)    pace = 6    print("Pace Is",pace)    defending = 2    print("Defending Is",defending)if pos == "MID":    shot = 6    print("Shot Is",shot)    passing = 6    print("Passing Is",passing)    pace = 6    print("Pace Is",pace)    defending = 4    print("Defending Is",defending)if pos == "DEF":    shot = 2    print("Shot Is",shot)    passing = 6    print("Passing Is",passing)    pace = 4    print("Pace Is",pace)    defending = 8    print("Defending Is",defending)if pos == "GK":    dive = 7    dist = 8    catch = 7print(pos)while pos != "ST" and "MID" and "DEF" and "GK" and "St" and "Mid" and "Def" and "Gk":    print("What Position Do You Want To Play?")    time.sleep(1)    print("The Options Are..")    time.sleep(1)    print("ST (Striker)")    time.sleep(1)    print("MID (Midfielder)")    time.sleep(1)    print("DEF (Defender)")    time.sleep(1)    print("GK (Goalkeeper)")    time.sleep(1)pos = input("What Is Your Choice")
查看完整描述

3 回答

?
鳳凰求蠱

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

這部分是錯誤的:

while pos != "ST" and "MID" and "DEF" and "GK" and "St" and "Mid" and "Def" and "Gk":

pos != "ST"被評估,其余的字符串不與任何東西進行比較。實際上,該部分的評估方式如下:

while (pos != "ST") and ("MID") and ("DEF") and ("GK") and ("St") and ("Mid") and ("Def") and ("Gk"):

非空字符串總是True,因此只要pos != "ST"True,它就永遠不會退出循環。你可能想做的是:

while pos != "ST" and pos != "MID" and pos != "DEF" and pos != "GK" and pos != "St" and pos != "Mid" and pos != "Def" and pos != "Gk":

但是,正如已經指出的評論之一,您可以使用in

while pos not in {"ST", "MID", "DEF", "GK", "St", "Mid", "Def", "Gk"}:

請注意,我在這里使用了一個集合,因為它們提供了更有效的成員資格測試。在這個小例子中可能無關緊要,但它仍然是一個更好的選擇。


查看完整回答
反對 回復 2021-08-17
?
慕的地8271018

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

while 循環永遠不會完成,因為您的輸入在外部。所以這是工作代碼:


import time

pos = ""



while pos != "ST" and "MID" and "DEF" and "GK" and "St" and "Mid" and "Def" and "Gk":

    print("What Position Do You Want To Play?")

    time.sleep(1)

    print("The Options Are..")

    time.sleep(1)

    print("ST (Striker)")

    time.sleep(1)

    print("MID (Midfielder)")

    time.sleep(1)

    print("DEF (Defender)")

    time.sleep(1)

    print("GK (Goalkeeper)")

    time.sleep(1)


    pos = input("What Is Your Choice")

    break



if pos == "ST":

    shot = 8

    print("Shot Is",shot)

    passing = 6

    print("Passing Is",passing)

    pace = 6

    print("Pace Is",pace)

    defending = 2

    print("Defending Is",defending)


if pos == "MID":

    shot = 6

    print("Shot Is",shot)

    passing = 6

    print("Passing Is",passing)

    pace = 6

    print("Pace Is",pace)

    defending = 4

    print("Defending Is",defending)


if pos == "DEF":

    shot = 2

    print("Shot Is",shot)

    passing = 6

    print("Passing Is",passing)

    pace = 4

    print("Pace Is",pace)

    defending = 8

    print("Defending Is",defending)


if pos == "GK":

    dive = 7

    dist = 8

    catch = 7


    print(pos)

你必須選擇“”,因為它是一個字符串


查看完整回答
反對 回復 2021-08-17
  • 3 回答
  • 0 關注
  • 227 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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