新牌输入部分不起作用 - BlackJack Game-Python

问题描述 投票:0回答:1

我用 python 设计了一款二十一点游戏,但有一个输入部分无法正常工作。 与玩二十一点 (21) 时的许多赌博游戏一样,可以选择提供新牌。所以我基本上通过输入来实现这一点并继续计算逻辑:

new_card = input("Do you want a new card? y or n: ").lower()
if new_card == "y":
    add_user(random_num())
    check()  #that gives the chance to get back and start with new hand.
else: 
    while comp_total < 17:
        add_comp(random_num())  # that adds to comp's hand new cards until the sum equal/over 17
    comp_total = sum(comp_cards)
    user_total = sum(user_cards)
    if comp_total > 21:
        win()  # Win and lose functions should print "you lose" and "you win" by logic
        clear() # That is a replit function that is work properly
    elif user_total > comp_total:
        win()
        clear()
    elif user_total < comp_total:
        lose()
        clear()
    else:
        print(f"Both of yours cards' total {user_total} and {comp_total}. It's a draw!")
        clear()

当用户输入“y”时,它可以正常工作。但如果用户输入“n”,它会清除屏幕并且不打印任何内容。 还有replit中的其余代码(这是清晰功能的重要细节):

import random
from art import logo
from replit import clear
user_cards = []
comp_cards = []
print(logo)

def random_num():
  cards = [11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10]
  card = random.choice(cards)
  return card
def add_user(card):
  user_cards.append(card)
def add_comp(card):
  comp_cards.append(card)
def win():
  print("You win!")
def lose():
  print("You lose")


add_user(random_num())
add_user(random_num())
add_comp(random_num())
add_comp(random_num())

def check():
  user_total = int(sum(user_cards))
  comp_total = int(sum(comp_cards))
  print(f"Your cards are {user_cards} and the total is {user_total}")
  print(f"Dealer's first card is {comp_cards[0]}")
  if user_total == 21:
    win()
    clear()
  if comp_total == 21:
    lose()
    clear()
  if user_total > 21 and 11 in user_cards:
    if user_total - 10 > 21:
      lose()
      clear()
  if user_total > 21 and 11 not in user_cards:
    lose()
    clear()
  new_card = input("Do you want a new card? y or n: ").lower()
  if new_card == "y":
    add_user(random_num())
    check()
  if new_card == "n": 
    while comp_total < 17:
      add_comp(random_num())
    comp_total = sum(comp_cards)
    user_total = sum(user_cards)
    if comp_total > 21:
      win()
      clear()
    elif user_total > comp_total:
      win()
      clear()
    elif user_total < comp_total:
      lose()
      clear()
    else:
      print(f"Both of yours cards' total {user_total} and {comp_total}. It's a draw!")
    clear()
check()

我尝试先做出 else 语句 if new_card == "n" (我知道这真的是无稽之谈,但我什至尝试过)。但显然又不起作用了。我也尝试过使用 chatgpt,但它给了我相同的答案。

正如我所说,我是初学者,我可能会错过一些小事情。但我不明白问题出在哪里。

python input blackjack
1个回答
0
投票
if comp_total > 21:
    win()
    clear()

在此代码中,您调用

win()
打印一条消息,然后调用
clear()
清除屏幕,因此消息立即消失。

其他案例也有类似的问题。

如果您希望消息保留在屏幕上,请不要拨打

clear()

© www.soinside.com 2019 - 2024. All rights reserved.