【问题标题】:TypeError: int() argument must be a string, a bytes-like object or a number, not 'tuple'TypeError: int() 参数必须是字符串、类似字节的对象或数字,而不是“元组”
【发布时间】:2018-03-17 18:26:54
【问题描述】:

我目前正在学习 Python 5 周,我正在尝试编写一个非常简化的 Blackjack 版本。我快完成了,但我无法通过这个特定的错误消息:

TypeError: int() argument must be a string, a bytes-like object or a number, not 'tuple'

代码如下:

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()

从 Python 开始,第 4 版,我只写了五章。所以我应该只使用前五章涵盖的原则。

【问题讨论】:

  • return() 这样的语句返回一个空元组()。如果您不想返回任何东西,请使用return。在你的情况下,return 0 可能更合适,不知道。
  • 对各种int(...) 电话感到好奇;除了可能用于用户输入(始终是字符串)之外,这似乎很少需要。
  • 这是在乞求打印语句进行调试。下次可能应该先这样做。
  • 谢谢,@Evert。将return() 更改为return 似乎改善了一些事情。但现在我收到TypeError: '&gt;' not supported between instances of 'NoneType' and 'int' 错误消息。你是对的,我认为大多数ints 是不必要的。那是我试图隔离问题的产物。

标签: python function tuples typeerror


【解决方案1】:

好的,感谢@Evert 和@Wiggy A.,我在get_player_score 函数中修复了return 语句。我意识到我需要将语句更改为return sum_player_cards,而不是return 0return。我认为return 语句只能在函数定义末尾使用时返回值。但它们也可以在ifelifelse 语句中使用。感谢您的意见。

【讨论】:

  • 请注意,如果没有适当的返回值,则引发异常可能是正确的做法(例如ValueError(...))。
猜你喜欢
  • 1970-01-01
  • 2023-03-29
  • 1970-01-01
  • 1970-01-01
  • 2021-01-14
  • 2017-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多