首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么" stop“输入不停止hitorstop()并运行条件elif todo == "stop”的代码?

为什么" stop“输入不停止hitorstop()并运行条件elif todo == "stop”的代码?
EN

Stack Overflow用户
提问于 2021-07-16 23:47:35
回答 1查看 12关注 0票数 0
代码语言:javascript
运行
复制
import random
class Player:
    deck = ["A♣", "2♣", "3♣", "4♣", "5♣", "6♣", "7♣", "8♣", "9♣", "10♣", "J♣", "K♣", "Q♣", "A♦", "2♦", "3♦", "4♦", "5♦",
            "6♦", "7♦", "8♦", "9♦", "10♦", "J♦", "K♦", "Q♦", "A♥", "2♥", "3♥", "4♥", "5♥", "6♥", "7♥", "8♥", "9♥",
            "10♥", "J♥", "K♥", "Q♥", "A♠", "2♠", "3♠", "4♠", "5♠", "6♠", "7♠", "8♠", "9♠", "10♠", "J♠", "K♠", "Q♠"]

    total = 0
    run = True
    def __init__(self):
        self.name = input("Enter your name : ")
        self.Bet = inputs.bet(self)
        self.Aceval = inputs.Aval(self)

class Dealer:
    deck = ["A♣", "2♣", "3♣", "4♣", "5♣", "6♣", "7♣", "8♣", "9♣", "10♣", "J♣", "K♣", "Q♣", "A♦", "2♦", "3♦", "4♦", "5♦",
            "6♦", "7♦", "8♦", "9♦", "10♦", "J♦", "K♦", "Q♦", "A♥", "2♥", "3♥", "4♥", "5♥", "6♥", "7♥", "8♥", "9♥",
            "10♥", "J♥", "K♥", "Q♥", "A♠", "2♠", "3♠", "4♠", "5♠", "6♠", "7♠", "8♠", "9♠", "10♠", "J♠", "K♠", "Q♠"]

    total = 0
class inputs:
    def bet(self):
        """
        takes in the bet from the user and stores it in the player class
        :return: output
        """
        self.bet = input("Enter the amount for your bet : ")
        output = self.bet
        if self.bet.isnumeric() == False:
            print("Use your monke brains and enter correct input")
            return inputs.bet(self)
        else:
            return int(output)

    def Aval(self):
        """
        Takes the value for ace and stores it in the player class
        :return: output
        """
        self.aval = input("Enter the value for ACE (1 or 10) : ")
        output = self.aval
        if self.aval.isnumeric() == False:
            print("Use your monke brains and enter correct input")
            return inputs.Aval(self)
        elif self.aval.isnumeric() == True:
            if self.aval in ["1", "10"]:
                return int(output)
            else:
                print("I understand you suffer braincell deficiency but I need you to fire up those 2 braincells you have and enter the proper number")
                return inputs.Aval(self)

    def valcalc(card):
        """
        takes the card for player.deck and returns the value of that card
        :return: card value
        """
        if card[0] in ("K", "Q", "J"):
            return 10
        elif card[0] == "A":
            return p.Aceval
        else:
            if len(card) > 2:
                return int(card[0:2])
            else:
                return int(card[0])

def hitorstop(todo):
    if todo.lower() == ("hit" or "stop"):
        if todo.lower() == "hit":
            pcard = random.choice(Player.deck)
            dcard = random.choice(Dealer.deck)
            print("\nYour card is : ", pcard)
            Player.deck.remove(pcard)
            Dealer.deck.remove(dcard)
            p.total += inputs.valcalc(pcard)
            d.total += inputs.valcalc(dcard)
            print("Your total is : ", p.total)
            if p.total > 21:
                print("You lost lol")
                return
            elif d.total > 21:
                print("You won , sheeesh")
                return
            elif (p.total == d.total) == 21:
                print("Its a tie")
                return
            else:
                hitorstop(input("\n\nDo you want to hit or stop : "))
        else:
            if todo.lower() == "stop":
                pnum = 21 - p.total
                dnum = 21 - d.total
                if dnum > pnum:
                    print(p.name, "wins")
                    return
                elif pnum > dnum:
                    print("You lost against a dealer bot  , such a shame")
                    return
                else:
                    print("Its a tie , you didnt win shit , lol")
                    return
            else:
                hitorstop(input("\n\nDo you want to hit or stop : "))
    else:
        hitorstop(input("\n\nDo you want to hit or stop : "))

p = Player()
d = Dealer()

pcard = random.choice(Player.deck)
dcard = random.choice(Dealer.deck)
print("\nYour card is : ", pcard)
print("Dealer's card is :" + str(dcard) + "+")
Player.deck.remove(pcard)
Dealer.deck.remove(dcard)
p.total += inputs.valcalc(pcard)
d.total += inputs.valcalc(dcard)
print("Your total is : ", p.total)

hitorstop(input("Do you want to hit or stop : "))

为什么当我输入==作为输入时,下面的todo stop代码会运行,如果我为hitorstop()函数.设置"stop“,它就会一次又一次地循环请求输入..

EN

Stack Overflow用户

回答已采纳

发布于 2021-07-16 23:52:20

您需要将行todo.lower() == ("hit" or "stop"):替换为todo.lower() in ("hit", "stop"):才能使其工作。但是,这个整个条件从一开始就是多余的,您可以将其删除。您不需要它,因为您稍后将输入与"hit“和"stop”进行比较,因此比较两次是无用的。

下面是替换方法:

代码语言:javascript
运行
复制
def hitorstop(todo):
    if todo.lower() == "hit":
        pcard = random.choice(Player.deck)
        dcard = random.choice(Dealer.deck)
        print("\nYour card is : ", pcard)
        Player.deck.remove(pcard)
        Dealer.deck.remove(dcard)
        p.total += inputs.valcalc(pcard)
        d.total += inputs.valcalc(dcard)
        print("Your total is : ", p.total)
        if p.total > 21:
            print("You lost lol")
            return
        elif d.total > 21:
            print("You won , sheeesh")
            return
        elif (p.total == d.total) == 21:
            print("Its a tie")
            return
        else:
            hitorstop(input("\n\nDo you want to hit or stop : "))
    elif todo.lower() == "stop":
        pnum = 21 - p.total
        dnum = 21 - d.total
        if dnum > pnum:
            print(p.name, "wins")
            return
        elif pnum > dnum:
            print("You lost against a dealer bot  , such a shame")
            return
        else:
            print("Its a tie , you didnt win shit , lol")
    else:
        hitorstop(input("\n\nDo you want to hit or stop : "))
票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68411851

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档