liuxuan07 发表于 2021-9-6 21:41:25

小白,求助大神个问题。

temp = input("不妨猜一下小甲鱼现在心里想的是哪个数字:")
while not temp.isdigit():
    print("抱歉,输入不合法,", end='')
    temp = input("请输入一个整数:")
times = 3
while (temp!=8)and(times>0):
    temp = input("请输入一个整数:")
    temp = int(temp)    为什么不打这一行,就说 not supported between instances of 'str' and 'int'?
    times=times-1
    if temp==8:
      print('恭喜')
    else:
      if temp>8:
            print('大了')
      else:
            print('小了')
      if times>0:
            print("再试一次,", end='')
      else:
            print("机会用完了~")
print("游戏结束,喜欢么?")      

冬雪雪冬 发表于 2021-9-6 21:56:23

input得到的是字符串,不能用== , > 和整数比较

白two 发表于 2021-9-6 21:59:14

本帖最后由 白two 于 2021-9-6 22:07 编辑

temp = ''
times = 3
while (temp != 8) and (times > 0):
    temp = input("不妨猜一下小甲鱼现在心里想的是哪个数字:")
    while not temp.isdigit():
      print("抱歉,输入不合法,", end='')
      temp = input("请输入一个整数:")
    temp = int(temp)
    if temp == 8:
      print('恭喜')
    else:
      if temp > 8:
            print('大了')
      else:
            print('小了')
      if times > 0:
            print("再试一次,", end='')
      else:
            print("机会用完了~")
    times -= 1
print("游戏结束,喜欢么?")



你先自己看看去理解,如果还是不懂欢迎追问。

傻眼貓咪 发表于 2021-9-7 22:04:26

def game():
    """
    不妨猜一下小甲魚現在心裡想的是哪裡一個數字
    """
    answer = 8; n = 3
    while n > 0:
      try:
            num = int(input("請輸入一個整數:")); n -= 1
            if num == answer: print("恭喜"); return
            elif num < answer:
                if n: print(f"小了,剩餘次數:{n}")
                else: print("機會用完"); return
            elif n: print(f"大了,剩餘次數:{n}")
            else: print("機會用完"); return
      except: print("輸入不合法"); continue

print(game.__doc__)
game()

505659671 发表于 2021-9-7 23:08:17

同学,下次麻烦把问题表述清楚一些哈,根据你红字部分,我猜测你说的打这一行,有两个意思,
其一,像二楼那样说的,int和str是两种不同类型,他们无法比较,除非把一种str类型转成int类型,但是多数情况下str转int还是比较冒险的。
其二,为什么要int(temp)这一行代码?input这个函数是接受来自外界的输入,那么输入的一定是字符串str类型,本题是猜数字,也就是int类型,所以需要通过int(temp)来把str类型转为int类型,才能进行下面的执行。
还有问题,请继续问

liuxuan07 发表于 2021-9-9 21:53:06

冬雪雪冬 发表于 2021-9-6 21:56
input得到的是字符串,不能用== , > 和整数比较

谢谢

liuxuan07 发表于 2021-9-9 21:55:12

白two 发表于 2021-9-6 21:59
你先自己看看去理解,如果还是不懂欢迎追问。

谢谢

liuxuan07 发表于 2021-9-9 21:56:13

505659671 发表于 2021-9-7 23:08
同学,下次麻烦把问题表述清楚一些哈,根据你红字部分,我猜测你说的打这一行,有两个意思,
其一,像二楼 ...

明白乐,谢谢啊。

1472局 发表于 2021-9-10 00:16:21

{:10_269:}
import random
secret = random.randint(1,10)
print ('-------------------我爱鱼C工作室--------------------')
temp = input('不妨猜一下小甲鱼心里想的是那个数字:')
guess = int(temp)
if guess == secret:
      print ('我曹,你是我心里的蛔虫吗?')
      print ('哼,猜对了也没有奖励')
whileguess != secret:
    if guess == secret:
       print ('我曹,你是我心里的蛔虫吗?')
       print ('哼,猜对了也没有奖励')
       print ('游戏结束,不玩了!')
    else:
         if guess > secret:
            print ('哥~大了大了')
         else:
            print ('哥~小了小了')
    temp = input('没关系,再来一次吧!:')
    guess = int(temp)
    if guess == secret:
      print ('我曹,你是我心里的蛔虫吗?')
      print ('哼,猜对了也没有奖励')
      print ('游戏结束,不玩了!')

我写的跟你的又不一样
页: [1]
查看完整版本: 小白,求助大神个问题。