互联网小白. 发表于 2023-7-7 20:16:29

求助

counts = 3
import random
anwser = random.randint(1,10)
print("小甲鱼想的是什么数字:",end=" ")
temp = input()
# 这种想法是因为 type(1) 会返回 <class 'int'>,如果 type(temp) 返回结果一致说明输入是整数。
while type(temp) != type(1):
    print("抱歉,输入不合法,", end='')
    temp = input("请输入一个整数:")
while counts > 0:
   temp = input()
   guess = int(temp)
   if guess == anwser:
      print("你是小甲鱼肚子里的蛔虫吗")
      print("答对了也没奖励")
      break
   else:
      if guess < anwser:
          print("你猜小了")
      else:
          print("你猜大了")
      if counts > 0:
         print("再试一次:",end="")
      else:
         print("机会用完了")
      counts = counts -1
print("游戏结束")
   
有大佬知道这个程序为啥一直跳不出第一个循环

歌者文明清理员 发表于 2023-7-7 20:23:32

本帖最后由 歌者文明清理员 于 2023-7-7 22:17 编辑

没有int,第9行
https://fishc.com.cn/forum.php?mod=redirect&goto=findpost&ptid=230580&pid=6279297

Ewan-Ahiouy 发表于 2023-7-7 20:23:46

本帖最后由 Ewan-Ahiouy 于 2023-7-7 20:27 编辑

因为input函数会返回字符串,type后返回class str,当然和chass int 不一样啦^_^

应将temp = input("请输入一个整数:")改为temp = int(input("请输入一个整数:"))

sfqxx 发表于 2023-7-7 20:28:11

这个程序一直跳不出第一个循环的原因是输入的temp始终无法转换为整数类型,导致循环条件一直成立。可以尝试以下方法解决该问题:

1. 确保输入的是数字类型。你可以在输入前对用户输入进行验证,确保输入的是合法的整数。

while True:
    temp = input("请输入一个整数:")
    if temp.isdigit():
      break
    else:
      print("抱歉,输入不合法,请重新输入。")
2. 另外,还需要注意循环的顺序。在第一个循环之后,应该将输入的temp转换为整数类型,而不是在循环的开始处使用它。

请根据上述建议修改程序,应该能够解决跳不出第一个循环的问题。

3.在第9行加上int:counts = 3
import random
anwser = random.randint(1,10)
print("小甲鱼想的是什么数字:",end=" ")
temp = input()
# 这种想法是因为 type(1) 会返回 <class 'int'>,如果 type(temp) 返回结果一致说明输入是整数。
while type(temp) != type(1):
    print("抱歉,输入不合法,", end='')
    temp = int(input("请输入一个整数:"))
while counts > 0:
   temp = input()
   guess = int(temp)
   if guess == anwser:
      print("你是小甲鱼肚子里的蛔虫吗")
      print("答对了也没奖励")
      break
   else:
      if guess < anwser:
          print("你猜小了")
      else:
          print("你猜大了")
      if counts > 0:
         print("再试一次:",end="")
      else:
         print("机会用完了")
      counts = counts -1
print("游戏结束")
   


求最佳答案{:10_254:}

isdkz 发表于 2023-7-7 22:51:04

这个问题是因为 input() 函数在Python中默认获取的是字符串类型的数据,而 type(temp) != type(1) 这个条件是用来检查输入是否为整数类型,所以你的代码会一直停留在第一个循环。

一种解决方案是在接收输入时立即尝试将其转换为整数,并使用 try / except 结构来处理可能出现的错误。代码如下:

counts = 3
import random
answer = random.randint(1,10)
print("小甲鱼想的是什么数字:",end=" ")
while True:
    temp = input()
    try:
      temp = int(temp)# 尝试将输入转换为整数
      break# 如果转换成功,就跳出循环
    except ValueError:# 如果转换失败,就打印错误信息,并继续循环
      print("抱歉,输入不合法,", end='')
      print("请输入一个整数:", end='')
while counts > 0:
   guess = temp
   if guess == answer:
      print("你是小甲鱼肚子里的蛔虫吗")
      print("答对了也没奖励")
      break
   else:
      if guess < answer:
          print("你猜小了")
      else:
          print("你猜大了")
      counts -= 1
      if counts > 0:
         print("再试一次:",end="")
         while True:
            temp = input()
            try:
                temp = int(temp)# 尝试将输入转换为整数
                break# 如果转换成功,就跳出循环
            except ValueError:# 如果转换失败,就打印错误信息,并继续循环
                print("抱歉,输入不合法,", end='')
                print("请输入一个整数:", end='')
      else:
         print("机会用完了")
print("游戏结束")


以上代码首先检查输入的类型,如果输入不是整数,就会要求用户重新输入。然后在猜数字的过程中也加入了这个检查,确保用户在每次猜测时都输入的是整数。

sfqxx 发表于 2023-7-8 13:27:24

isdkz 发表于 2023-7-7 22:51
这个问题是因为 input() 函数在Python中默认获取的是字符串类型的数据,而 type(temp) != type(1) 这个条件 ...

{:10_249:}

isdkz 发表于 2023-7-8 13:32:06

sfqxx 发表于 2023-7-8 13:27


{:10_256:}

sfqxx 发表于 2023-7-8 13:35:37

isdkz 发表于 2023-7-8 13:32


{:10_266:},我快赶不上陶远航了
页: [1]
查看完整版本: 求助