叶夜还 发表于 2021-4-17 12:26:24

猜数游戏猜中时报错怎么回事,求大佬康康

"""use python make first game"""
import random
counts = 1
answer = random.randint(1,10)
while counts < 10:
    temp = input("")
    guess = int(temp)
    if guess < answer:
      print("遗憾,太小了")
    elif guess > answer:
      print("遗憾,太大了")
    else:
      print("预测"&counts&"次,你猜中了!")
      break
    counts = counts + 1
猜中时老报错这是咋回事里?求大佬解答
报错信息:
Traceback (most recent call last):
File "H:\系统默认\桌面\工作\Python工程\game.py", line 13, in <module>
    print("预测"&counts&"次,你猜中了!")
TypeError: unsupported operand type(s) for &: 'str' and 'int'

yuxijian2020 发表于 2021-4-17 12:31:52

print("预测"&counts&"次,你猜中了!")

改成

print("预测" + str(counts) + "次,你猜中了!")

逃兵 发表于 2021-4-17 13:28:42

"""use python make first game"""
import random
counts = 1
answer = random.randint(1,10)
while counts < 10:
    temp = input("")
    guess = int(temp)
    if guess < answer:
      print("遗憾,太小了")
    elif guess > answer:
      print("遗憾,太大了")
    else:
      print(f"预测{counts}次,你猜中了!")
      break
    counts = counts + 1

荆棘千千 发表于 2021-4-17 15:52:05

一楼正确
页: [1]
查看完整版本: 猜数游戏猜中时报错怎么回事,求大佬康康