无法将“tuple”对象隐式转换为str

2024-09-28 03:14:59 发布

您现在位置:Python中文网/ 问答频道 /正文

a=int(input("please input number of players  "))
while (a < 2):
a=int(input("please input at least two players  "))
if (a==2):
p1=input("please enter name for player 1  ")
p2=input("please enter name for player 2  ")
x=float(input("please enter initiative for "+(p1)))
y=float(input("please enter initiative for "+(p2)))
x=x,p1
y=y,p2
if (x > y):
    lowest=y
    highest=x
elif(y > x):
    lowest=x
    highest=y
print(lowest)
steps=int(input("Please enter number of steps for "+(lowest)+" action" ))

我已经想尽了一切办法,但我想不出该怎么做


Tags: ofnamenumberforinputiffloatint
1条回答
网友
1楼 · 发布于 2024-09-28 03:14:59

这正是消息所说的:在最后一行,在子表达式中

"Please enter number of steps for "+(lowest)+" action" 

您正在尝试将字符串与元组(lowest)连接,因为它要么是x,要么是y,并且由于分配x=x,p1y=y,p2,这两个元组都是元组,这需要从元组到字符串的隐式转换。

要解决这个问题,您必须显式地将元组转换为字符串(str(lowest))(尽管我怀疑您实际上只想显示元组的一个元素)。

相关问题 更多 >

    热门问题