求助这个报错怎么解决
采用2分法让电脑猜出随机数。报错:Traceback (most recent call last):File "D:/Python项目代码/2分法.py", line 30, in <module>
t = (t + m)/2
TypeError: unsupported operand type(s) for +: 'int' and 'tuple'
import random
answer = random.randint(1, 100)
i = 0
t = 0
m = int(1)
n = int(100)
t = (m + n)/2
t = int(t)
while 1:
print(t)
i = i + 1
if answer == t:
print("猜对了^_^")
print("一共猜了",i,"次")
break
else:
if t < answer:
print("小啦~")
m = int(t),
t = (t + n)/2
t = int(t)
else:
print("大啦!")
n = int(t)
t = (t + m)/2
t = int(t)
print('不玩了') import random
answer = random.randint(1, 100)
i = 0
t = 0
m = int(1)
n = int(100)
t = (m + n)/2
t = int(t)
while 1:
print(t)
i = i + 1
if answer == t:
print("猜对了^_^")
print("一共猜了",i,"次")
break
else:
if t < answer:
print("小啦~")
m = int(t) # 你的代码中这里多了一个逗号,使得 m 成为了元组,因为整数不能跟元组相加,从而出错
t = (t + n)/2
t = int(t)
else:
print("大啦!")
n = int(t)
t = (t + m)/2
t = int(t)
print('不玩了') isdkz 发表于 2023-3-2 19:20
谢谢大佬
页:
[1]