[英]TypeError: int() argument must be a string, a bytes-like object or a number, not 'tuple'
I am currently five weeks into learning Python, and I am trying to program a very simplified version of Blackjack. 目前,我要学习Python五个星期,并且正在尝试编写一个非常简化的Blackjack版本。 I am close to done, but I cannot get past this certain error message:
我快完成了,但是我无法摆脱以下错误消息:
TypeError: int() argument must be a string, a bytes-like object or a number, not 'tuple'
Here is the code: 这是代码:
import random
print("Welcome to my Black Jack program! Let's play!\n")
def deal_card():
Jack = 10
Queen = 10
King = 10
Ace = 1
cards = [Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King]
drawn_card = cards[random.randrange(1, 13)]
return drawn_card
def get_player_score():
first_player_card = deal_card()
second_player_card = deal_card()
sum_player_cards = first_player_card + second_player_card
print ("Your card total is: ", sum_player_cards, ".", sep="")
while sum_player_cards < 21:
choice = int(input("Would you like to hit or stay? Enter 1 for 'hit' or 2 for 'stay'. "))
if choice == 1:
new_card = deal_card()
sum_player_cards = sum_player_cards + new_card
print ("Your new total is: ", sum_player_cards, ".", sep="")
elif choice == 2:
return()
else:
print("Please choose 'hit' or stay'.")
choice = input("Would you like to hit or stay? Enter 1 for 'hit' or 2 for 'stay'. ")
if sum_player_cards > 21:
return()
return int(sum_player_cards)
def get_dealer_score():
first_dealer_card = deal_card()
second_dealer_card = deal_card()
sum_dealer_cards = int(first_dealer_card + second_dealer_card)
while sum_dealer_cards <= 16:
another_dealer_card = deal_card()
sum_dealer_cards = sum_dealer_cards + another_dealer_card
if sum_dealer_cards > 16:
print("The dealer's card total is: ", sum_dealer_cards, ".", sep="")
return int(sum_dealer_cards)
def main():
player_score = get_player_score()
dealer_score = get_dealer_score()
if player_score > dealer_score and player_score <= 21:
print("You win!")
elif dealer_score > player_score and dealer_score <= 21:
print("The dealer wins!")
elif dealer_score <= 21 and player_score > 21:
print("You've gone bust! Dealer wins!")
elif dealer_score > 21:
print("The dealer busts! You win!")
main()
I am barely five chapters into Starting Out with Python , 4th ed. 我刚读完《 Python入门》 (第4版)时只有5章。 So I should only be using principles covered through those first five chapters.
因此,我应该只使用前五章介绍的原理。
Okay, thanks to @Evert and @Wiggy A., I fixed the return
statements in my get_player_score
function. 好的,感谢@Evert和@Wiggy A.,我在
get_player_score
函数中修复了return
语句。 Instead of return 0
or return
, I realized I needed to change the statements to return sum_player_cards
. 而不是
return 0
或return
,我意识到我需要将语句更改为return sum_player_cards
。 I thought that return
statements could only return values when used at the end of a function definition. 我认为
return
语句只能在函数定义的末尾使用时返回值。 But they can be used in if
, elif
, and else
statements as well. 但是它们也可以在
if
, elif
和else
语句中使用。 Thanks for the input. 感谢您的输入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.