简体   繁体   English

TypeError:int()参数必须是字符串,类似字节的对象或数字,而不是“元组”

[英]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 0return ,我意识到我需要将语句更改为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. 但是它们也可以在ifelifelse语句中使用。 Thanks for the input. 感谢您的输入。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 int()参数必须是字符串,类似字节的对象或数字,而不是“元组” - int() argument must be a string, a bytes-like object or a number, not 'tuple' 类型错误:将形状转换为 TensorShape 时出错:int() 参数必须是字符串、类似字节的对象或数字,而不是“元组”。 在蟒蛇 - TypeError: Error converting shape to a TensorShape: int() argument must be a string, a bytes-like object or a number, not 'tuple'. in python Django保存到数据库:TypeError:int()参数必须是字符串,类似字节的对象或数字,而不是“元组” - Django save to DB: TypeError: int() argument must be a string, a bytes-like object or a number, not 'tuple' 将元组转换为 integer Python,错误信息:TypeError: int() argument must be a string, a bytes-like object or a real number, - Convert a tupple into integer Python, error message: TypeError: int() argument must be a string, a bytes-like object or a real number, not 'tuple' Python Tesseract:int() 参数必须是字符串、类似字节的对象或数字,而不是“元组” - Python Tesseract: int() argument must be a string, a bytes-like object or a number, not 'tuple` TypeError: int() 参数必须是一个字符串,一个类似字节的 object 或一个数字,而不是“NoneType”。 但我写了 int() - TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'. But I wrote the int() int() 参数必须是字符串、类似字节的对象或数字,而不是 &#39;QueryDict&#39; - int() argument must be a string, a bytes-like object or a number, not 'QueryDict' int()参数必须是字符串,类似字节的对象或数字,而不是“列表” - int() argument must be a string, a bytes-like object or a number, not 'list' int() 参数必须是字符串、类似字节的对象或数字,而不是“NoneType” - int() argument must be a string, a bytes-like object or a number, not 'NoneType' int() 参数必须是字符串、类似字节的对象或数字,而不是“QuerySet” - int() argument must be a string, a bytes-like object or a number, not 'QuerySet'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM