鱼C论坛

 找回密码
 立即注册
查看: 1421|回复: 3

[已解决]猜数游戏猜中时报错怎么回事,求大佬康康

[复制链接]
发表于 2021-4-17 12:26:24 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. """use python make first game"""
  2. import random
  3. counts = 1
  4. answer = random.randint(1,10)
  5. while counts < 10:
  6.     temp = input("")
  7.     guess = int(temp)
  8.     if guess < answer:
  9.         print("遗憾,太小了")
  10.     elif guess > answer:
  11.         print("遗憾,太大了")
  12.     else:
  13.         print("预测"&counts&"次,你猜中了!")
  14.         break
  15.     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'
最佳答案
2021-4-17 12:31:52
  1. print("预测"&counts&"次,你猜中了!")
复制代码


改成

  1. print("预测" + str(counts) + "次,你猜中了!")
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-4-17 12:31:52 | 显示全部楼层    本楼为最佳答案   
  1. print("预测"&counts&"次,你猜中了!")
复制代码


改成

  1. print("预测" + str(counts) + "次,你猜中了!")
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-4-17 13:28:42 | 显示全部楼层
  1. """use python make first game"""
  2. import random
  3. counts = 1
  4. answer = random.randint(1,10)
  5. while counts < 10:
  6.     temp = input("")
  7.     guess = int(temp)
  8.     if guess < answer:
  9.         print("遗憾,太小了")
  10.     elif guess > answer:
  11.         print("遗憾,太大了")
  12.     else:
  13.         print(f"预测{counts}次,你猜中了!")
  14.         break
  15.     counts = counts + 1
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-4-17 15:52:05 | 显示全部楼层
一楼正确
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-17 00:47

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表