鱼C论坛

 找回密码
 立即注册
查看: 2240|回复: 9

[已解决]新人提问

[复制链接]
发表于 2020-3-17 21:37:09 | 显示全部楼层 |阅读模式

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

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

x
学习第二天
看了小甲鱼的课后题,答案中有这么一段代码
temp = input("不妨猜一下小甲鱼现在心里想的是哪个数字:")
# 这种想法是因为 type(1) 会返回 <class 'int'>,如果 type(temp) 返回结果一致说明输入是整数。
while type(temp) != type(1):
    print("抱歉,输入不合法,", end='')
    temp = input("请输入一个整数:")

我想知道
while type(temp) != type(1):
这里面的1是随便写的 只是为了判断temp的类型是否为整数还是有什么特殊含义?
最佳答案
2020-3-17 21:38:36
其实,这段代码是错的!!小甲鱼后面应该会有说

type(1) 还可以换成 type(2), type(3), ... ,结果都是 int,所以 type(temp) != type(1) 可以直接写成 type(temp) != int
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-3-17 21:38:36 | 显示全部楼层    本楼为最佳答案   
其实,这段代码是错的!!小甲鱼后面应该会有说

type(1) 还可以换成 type(2), type(3), ... ,结果都是 int,所以 type(temp) != type(1) 可以直接写成 type(temp) != int
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 3 反对 0

使用道具 举报

 楼主| 发表于 2020-3-17 21:41:26 | 显示全部楼层
zltzlt 发表于 2020-3-17 21:38
其实,这段代码是错的!!小甲鱼后面应该会有说

type(1) 还可以换成 type(2), type(3), ... ,结果都是  ...
print("抱歉,输入不合法,", end='')

这个end=是啥意思 是不是前面的教程还没有学到
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-17 21:41:51 | 显示全部楼层
zltzlt 发表于 2020-3-17 21:38
其实,这段代码是错的!!小甲鱼后面应该会有说

type(1) 还可以换成 type(2), type(3), ... ,结果都是  ...

楼上说的没错
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-17 21:48:25 | 显示全部楼层
1楼说的没错
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-17 21:49:34 | 显示全部楼层
zltzlt 发表于 2020-3-17 21:38
其实,这段代码是错的!!小甲鱼后面应该会有说

type(1) 还可以换成 type(2), type(3), ... ,结果都是  ...


看下我三楼第二个问题呗
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-17 23:34:32 | 显示全部楼层
hu2008xin 发表于 2020-3-17 21:49
看下我三楼第二个问题呗

用空格来代替换行,这样就可以继续输入了(在同一行,形式上用户好接受,不然又要另起一行)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-18 07:55:24 | 显示全部楼层
hu2008xin 发表于 2020-3-17 21:41
这个end=是啥意思 是不是前面的教程还没有学到

请见:https://fishc.com.cn/thread-159045-1-1.html
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-18 08:16:34 | 显示全部楼层
完整游戏代码1-50(可以猜5次)
  1. import random
  2. secret = random.randint(1,50)
  3. print("-------------Guess number game--------------")
  4. temp = input("Guess what number I am thinking in 1-50 (try 5 times) : ")
  5. guess = int(temp)
  6. if guess == secret:
  7.         print("You get it!")
  8.         print("It was it!")
  9. else:
  10.         if guess > secret:
  11.             print("It was bigger than the number")
  12.         else:
  13.             print("It was smaller than the number")
  14. counts = 4
  15. if guess != secret:
  16.         while counts > 0:
  17.                 temp = input("Incorrect ,try again: ")
  18.                 guess = int(temp)
  19.                 if guess == secret:
  20.                         print("You get it!")
  21.                         print("It was it! ")
  22.                 else:
  23.                         if guess > secret:
  24.                             print("It was bigger than the number")
  25.                         else:
  26.                             print("It was smaller than the number")
  27.                 counts = counts - 1
  28. print("Game Over^_^")
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-20 17:57:08 | 显示全部楼层
wangka 发表于 2020-3-18 08:16
完整游戏代码1-50(可以猜5次)

谢谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-8 10:18

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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