【问题标题】:Can't convert 'int' object to str implicitly?不能将“int”对象隐式转换为 str?
【发布时间】:2017-10-10 19:57:45
【问题描述】:

我正在制作石头剪刀布,当我运行它时,它显示“无法将 'int' 对象隐式转换为 str”。

import time
import random
print("Do you want to play rock, paper, scissors?")
playerscore = 0
cpuscore = 0
game = input()
game = game.upper()
while game == "SURE" or game == "YES" or game == "YEAH": # starts rock                                 paper scissors
    number = random.randint(1, 3)
    if number == 1:
        cpurpors = "SCISSORS"
    elif number == 2:
        cpurpors = "ROCK"
    elif number == 3:
        cpurpors = "PAPER"
    print("Cool! Rock, paper or scissors?")
    rpors = input()
    rpors = rpors.upper()
    print("Rock")
    time.sleep(.5)
    print("Paper")
    time.sleep(.5)
    print("Scissors")
    time.sleep(.5)
    print(cpurpors + "!")
    time.sleep(.5)
    if cpurpors == rpors:
        print("Draw!")
    elif cpurpors == "SCISSORS" and rpors == "PAPER" or cpurpors == "PAPER" and rpors == "ROCK" or cpurpors == "ROCK" and rpors == "SCISSORS":
        cpuscore = cpuscore + 1
        print("Haha, I win!")
    elif rpors == "SCISSORS" and cpurpors == "PAPER" or rpors == "PAPER" and cpurpors == "ROCK" or rpors == "ROCK" and cpurpors == "SCISSORS":
        playerscore = playerscore + 1
        print("Oh no! You win!")
    print("The scores are:")
    print("Guiseppebot: " + cpuscore)
    print(name + ": " + playerscore)
    print("Would you like to play again?")
    game = input()
    game = game.upper()

【问题讨论】:

标签: python


【解决方案1】:

问题是您将int 变量添加到打印语句中。尝试做这样的事情:

print(name + ": " + str(playerscore))

在该示例中,您将playerscore 设置为string 版本的int

【讨论】:

    【解决方案2】:

    需要用str 包装整数cpuscoreplayerscore 以将它们转换为字符串,因为它不会隐式执行。

    print("Guiseppebot: " + str(cpuscore))
    print(name + ": " + str(playerscore))
    

    另一个不错的选择是使用格式,它会为您调用对象上的str,因此您将来不必担心它:

    print("Guiseppebot: {}".format(cpuscore))
    print("{}: {}".format(name, playerscore))
    

    str 是将非字符串对象转换为字符串的内置函数。它通过调用对象 .__str__ 方法(如果它存在)来实现这一点(并且知道如何处理 int 和 float 等对象。)在此处的文档中阅读更多相关信息 https://docs.python.org/3/library/stdtypes.html#str

    【讨论】:

    • 请解释一下str是什么意思?
    • @WSingleton 当然,添加到答案:str 是将非字符串对象转换为字符串的内置函数。它通过调用对象__str__ 方法(如果它存在)来实现这一点(并且知道如何处理int 和float 等对象。)
    【解决方案3】:

    我不确定这是否是最好的解决方案,但我的方法是使用 str() 将整数(cpucore)转换为字符串。

    import time
    import random
    name = input ("what's your name? ") #added in input for variable (name)
    print("Do you want to play rock, paper, scissors?")
    playerscore = 0
    cpuscore = 0
    game = input()
    game = game.upper()
    while game == "SURE" or game == "YES" or game == "YEAH": # starts rock                                 paper scissors
        number = random.randint(1, 3)
        if number == 1:
            cpurpors = "SCISSORS"
        elif number == 2:
            cpurpors = "ROCK"
        elif number == 3:
            cpurpors = "PAPER"
        print("Cool! Rock, paper or scissors?")
        rpors = input()
        rpors = rpors.upper()
        print("Rock")
        time.sleep(.5)
        print("Paper")
        time.sleep(.5)
        print("Scissors")
        time.sleep(.5)
        print(cpurpors + "!")
        time.sleep(.5)
        if cpurpors == rpors:
            print("Draw!")
        elif cpurpors == "SCISSORS" and rpors == "PAPER" or cpurpors == "PAPER" and rpors == "ROCK" or cpurpors == "ROCK" and rpors == "SCISSORS":
            cpuscore = cpuscore + 1
            print("Haha, I win!")
        elif rpors == "SCISSORS" and cpurpors == "PAPER" or rpors == "PAPER" and cpurpors == "ROCK" or rpors == "ROCK" and cpurpors == "SCISSORS":
            playerscore = playerscore + 1
            print("Oh no! You win!")
        print("The scores are:")
        print("Guiseppebot: " + str(cpuscore)) #converted cpuscore to a string
        print(name + ": " + str(playerscore)) #variable (name) wasn't declared
        print("Would you like to play again?")
        game = input()
        game = game.upper()
    

    【讨论】:

      【解决方案4】:

      cpuscore 是一个整数(int),它不能与字符串连接。 Python 不会自动将其转换为字符串(“隐式”)。所以你至少需要做:

      print("Guiseppebot: " + str(cpuscore))
      

      更好/更简单,在 Python 3.6+ 中,使用 f-strings 假设您可以将变量中的值放入字符串(整数、字符串、列表、字典都可以),您只需将它们放在大括号内。

      下面是带有该错误的行在 f-strings 中的样子:

      print(f"Guiseppebot: {cpuscore}")
      

      【讨论】:

        猜你喜欢
        • 2012-11-19
        • 2017-08-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-21
        • 2015-03-23
        • 2017-07-21
        相关资源
        最近更新 更多