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

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

如何將我自己的函數放在 if 語句中?

如何將我自己的函數放在 if 語句中?

夢里花落0921 2021-09-28 20:35:53
我正在嘗試制作一個簡單的二十一點游戲,我希望能夠創建自己的功能,這已經可以做到,然后將其放在 if 語句中,以便如果用戶想要“站立”,則它運行該功能“站立”。但是,當 python 讀取代碼時,即使用戶說“命中”,它也會看到所有函數并運行所有函數。def stand():    print("You have chosen to Stand")    #my stand code will go heredef hit():    print("You have chosen to Hit")    #my Hit code will go heredef doubledown():    print("You have chosen to Double Down")    #my Double Down code will go heredef step():    step = input("What would you like to do now? Stand, Hit or Double Down")    if step == ("Stand") or ("stand"):        stand()    if step == ("Hit") or ("hit"):        hit()    if step == ("Double down") or ("down"):        doubledown()    else:        step()step()我希望用戶能夠一次運行 'Hit'、'double down' 或 'stand' 功能 1,而無需所有這些功能都運行。
查看完整描述

2 回答

?
守候你守候我

TA貢獻1802條經驗 獲得超10個贊

def stand():

    print("You have chosen to Stand")

    #my stand code will go here

def hit():

    print("You have chosen to Hit")

    #my Hit code will go here

def doubledown():

    print("You have chosen to Double Down")

    #my Double Down code will go here

def step():

    step = input("What would you like to do now? Stand, Hit or Double Down")

    if (step == "Stand" or step== "stand"):

        stand()

    elif (step == "Hit" or step=="hit"):

        hit()

    elif (step == "Double down" or step=="down"):

        doubledown()

    else:

        step()

step()

問題在于 if 語法。


查看完整回答
反對 回復 2021-09-28
?
九州編程

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

因為在這里錯誤地使用了 if 語句,例如“if step == ("Hit") or ("hit"):” python 將執行 step==("Hit") 并且根據用戶輸入結果為 False 或 True但它跟隨字符串“hit”python 會像 True 一樣讀取它,所以最后,它就像“if (step==(“Hit”)) or (True) ,那么你的每一個 if 語句都會被執行,因為邏輯上是 True !

你應該像 if (step==sth) 或 (step==sth) 一樣改變你的代碼


查看完整回答
反對 回復 2021-09-28
  • 2 回答
  • 0 關注
  • 292 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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