vendredi 20 décembre 2019

Need to add a function to the program; noted there are many if-else statements [closed]

I am in the process of writing blackjack in python. You need to replace chunks of repetitive code with a function. When trying to do this, I run into the problem of global and local variables.

I need to create one function that will return the current number of player cards. I will add dealer cards and the amount of bets later. And also I know in advance that this code is terrible, but I'm only new to learning python.

Code:

    import random

    xxc = 0
    ish = 1
    i = 0
    cards = [2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11] * 4 * 8

The next part is full of if-else statements:

    def card():
      a = random.randrange(1, 416, 1)
      return cards[a]

    a = card()
    b = card()
    if a == 11 and b != 11: 
      i = 1
      c = 11 + b
      d = 1 + b
      player_cards = (f"{c} or {d}")
    elif b == 11 and a != 11:
      i = 1
      c = 11 + a
      d = 1 + a
      player_cards = (f"{c} or {d}") 
    elif a == 11 and b == 11:
      player_cards = 12
    else:
      player_cards = a + b

    if (c == 21 or d == 21) and i == 1:
      print("Вы выиграли!")
      ish = 0
    if ish != 0:
      z = input(f"У вас {player_cards} очков. Будете брать ещё? y или n: ")
      if z == "y" and ish != 0:
        a = card()
        print(a)
        if a == 11 and i == 1:
          c = c + 1
          d = d + 1
          player_cards = (f"{c} or {d}")

        elif a == 11 and i == 0:
          i = 1
          c = 11 + player_cards
          d = 1 + player_cards
          player_cards = (f"{c} or {d}")

        elif a != 11 and i == 1:
          c = c + a
          d = d + a
          player_cards = (f"{c} or {d}")

        else:
          player_cards = player_cards + a

        if i == 0:
          if player_cards > 21:
            print("Вы проиграли")
            ish = 0
          if player_cards == 21:
            print("Вы выиграли")
            ish = 0
        if (c == 21 or d == 21) and i == 1:
          print("Вы выиграли!")
          ish = 0
        if i == 1:
          if d > 21:
            player_cards = c
            xxc = 1
          if c > 21:
            player_cards = d
            xxc = 1
          if c > 21 and d > 21:
            print("Вы проиграли")
            ish = 0
            if c == d:
              player_cards = c
            elif c > d:
              player_cards = d
            elif c < d:
              player_cards = c
        if ish != 0:

          cer = input(f"У вас {player_cards} очков. Будете брать ещё? y или n: ")
          if cer == "n":
            if i == 1 and xxc != 1:
              if c == d:
                player_cards = c
              elif c > d:
                player_cards = c
              elif c < d:
                player_cards = d
            print(f"У вас {player_cards} очков")
          if cer == "y":
            a = card()
            print(a)
            print(i)
            if a == 11 and i == 1:
              c = c + 1
              d = d + 1
              player_cards = (f"{c} or {d}")

            elif a == 11 and i == 0:
              i = 1
              c = 11 + player_cards
              d = 1 + player_cards
              player_cards = (f"{c} or {d}")

            elif a != 11 and i == 1:
              c = c + a
              d = d + a
              player_cards = (f"{c} or {d}")

            else:
              player_cards = player_cards + a

            if i == 0:
              if player_cards > 21:
                print("Вы проиграли")
                ish = 0

            if (c == 21 or d == 21) and i == 1:
              print("Вы выиграли!")
              ish = 0
            if i == 1:
              if d > 21:
                player_cards = c
                xxc = 1
              if c > 21:
                player_cards = d
                xxc = 1
              if c > 21 and d > 21:
                print("Вы проиграли")
                ish = 0
                if c == d:
                  player_cards = c
                elif c > d:
                  player_cards = d
                elif c < d:
                  player_cards = c
            if ish != 0:
              vvs = input(f"У вас {player_cards} очков. Будете брать ещё? y или n: ")

              if vvs =='y':
                a = card()
                print(a)
                print(i)
                if a == 11 and i == 1:
                  c = c + 1
                  d = d + 1
                  player_cards = (f"{c} or {d}")

                elif a == 11 and i == 0:
                  i = 1
                  c = 11 + player_cards
                  d = 1 + player_cards
                  player_cards = (f"{c} or {d}")

                elif a != 11 and i == 1:
                  c = c + a
                  d = d + a
                  player_cards = (f"{c} or {d}")

                else:
                  player_cards = player_cards + a

                if i == 0:
                  if player_cards > 21:
                    print("Вы проиграли")
                    ish = 0

                if (c == 21 or d == 21) and i == 1:
                  print("Вы выиграли!")
                  ish = 0
                if i == 1:
                  if d > 21:
                    player_cards = c
                    xxc = 1
                  if c > 21:
                    player_cards = d
                    xxc = 1
                  if c > 21 and d > 21:
                    print("Вы проиграли")
                    ish = 0
                    if c == d:
                      player_cards = c
                    elif c > d:
                      player_cards = d
                    elif c < d:
                      player_cards = c
                if ish != 0:
                  vvsn = input(f"У вас {player_cards} очков. Будете брать ещё? y или n: ")
                  if vvsn == 'y':
                    a = card()
                    print(a)
                    print(i)
                    if a == 11 and i == 1:
                      c = c + 1
                      d = d + 1
                      player_cards = (f"{c} or {d}")

                    elif a == 11 and i == 0:
                      i = 1
                      c = 11 + player_cards
                      d = 1 + player_cards
                      player_cards = (f"{c} or {d}")

                    elif a != 11 and i == 1:
                      c = c + a
                      d = d + a
                      player_cards = (f"{c} or {d}")

                    else:
                      player_cards = player_cards + a

                    if i == 0:
                      if player_cards > 21:
                        print("Вы проиграли")
                        ish = 0

                    if (c == 21 or d == 21) and i == 1:
                      print("Вы выиграли!")
                      ish = 0
                    if i == 1:
                      if d > 21:
                        player_cards = c
                        xxc = 1
                      if c > 21:
                        player_cards = d
                        xxc = 1
                      if c > 21 and d > 21:
                        print("Вы проиграли")
                        ish = 0
                        if c == d:
                          player_cards = c
                        elif c > d:
                          player_cards = d
                        elif c < d:
                          player_cards = c
                    if ish != 0:
                      kpss = input(f"У вас {player_cards} очков. Будете брать ещё? y или n: ")
                      if kpss == 'n':
                        print(f"У вас {player_cards} очков.")

                  if vvsn == 'n':
                    print(f"У вас {player_cards} очков.")

              if vvs == 'n':
                print(f"У вас {player_cards} очков.")

      if z == "n":
        if i == 1 and xxc != 1:
          if c == d:
            player_cards = c
          elif c > d:
            player_cards = c
          elif c < d:
            player_cards = d
        print(f"У вас {player_cards} очков")

Aucun commentaire:

Enregistrer un commentaire