sách gpt4 ai đã đi

python - Blackjack 风格的 Python(计算机)游戏循环不正确

In lại 作者:行者123 更新时间:2023-12-01 05:29:51 34 4
mua khóa gpt4 Nike

我遇到的主要问题是当我选择 stay 时会发生什么上hand_one ,Sau đó hitthượng đẳnghand_two .

而不是让我hit or staythượng đẳnghand_two再次,它让我回到hit or staythượng đẳnghand_one ,当我已经选择留在 hand_one 时,所以 hand_one 应该没有更多选择。这会导致出现多个打印语句和不正确的游戏的问题。

我的代码有什么问题,就像导致它循环回到 hand_one .
完整代码在这里:http://labs.codecademy.com/Bjaf/2#:workspace

这是可能导致问题的部分。

def hit_or_stay(person):
hit_or_stay = raw_input("Do you want to hit or stay? You can type h or s.")
if hit_or_stay == 'h' or hit_or_stay == 'hit':
deal_one_card(person)
value_of_current_cards(person)
number_value_of_hand()
elif hit_or_stay == 's'or hit_or_stay == 'stay':
print "You stayed"
trở lại
khác:
hit_or_stay(person)

def number_value_of_hand():
if number_of_hands > 0:
value_of_hand_one = value_of_current_cards(hand_one)
if value_of_hand_one < 18:
print "\n" "You have %i" % (value_of_hand_one)
hit_or_stay(hand_one)
elif value_of_hand_one > 18:
print "You Lose"
trở lại
elif value_of_hand_one == 18:
print "You hit HOT 18!!!"
trở lại
if number_of_hands > 1:
value_of_hand_two = value_of_current_cards(hand_two)
if value_of_hand_two < 18:
print "\n" "Your second hand has %i" % (value_of_hand_two)
hit_or_stay(hand_two)
elif value_of_hand_two > 18:
print "You Lose"
trở lại
elif value_of_hand_two == 18:
print "You hit HOT 18!!!"
trở lại

number_value_of_hand()

谁能明白为什么它会循环回来给hand_one另一个选择?以及我该如何解决它?非常感谢!

1 Câu trả lời

您的问题出现在这一步:

hit_or_stay(hand_two)

当你点击 hand_two 时,你的代码会执行以下操作:

deal_one_card(person)
value_of_current_cards(person)
number_value_of_hand()

问题就在那里,因为 number_value_of_hand() 将您带回到该函数的开头,并再次遍历 hand_one 选项。

您可能需要重写 number_value_of_hand() 函数以包含一个参数,告诉它从哪里开始(hand_one、hand_two 等)

我可能会创建一个手牌列表,并遍历该列表。然后,您可以调用 number_of_hands(hands[i]) 来获取第 Tôi 手。

关于python - Blackjack 风格的 Python(计算机)游戏循环不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20481181/

34 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com