hu2008xin 发表于 2020-3-17 21:37:09

新人提问

学习第二天
看了小甲鱼的课后题,答案中有这么一段代码
temp = input("不妨猜一下小甲鱼现在心里想的是哪个数字:")
# 这种想法是因为 type(1) 会返回 <class 'int'>,如果 type(temp) 返回结果一致说明输入是整数。
while type(temp) != type(1):
    print("抱歉,输入不合法,", end='')
    temp = input("请输入一个整数:")
我想知道
while type(temp) != type(1):这里面的1是随便写的 只是为了判断temp的类型是否为整数还是有什么特殊含义?

zltzlt 发表于 2020-3-17 21:38:36

其实,这段代码是错的!!小甲鱼后面应该会有说

type(1) 还可以换成 type(2), type(3), ... ,结果都是 int,所以 type(temp) != type(1) 可以直接写成 type(temp) != int

hu2008xin 发表于 2020-3-17 21:41:26

zltzlt 发表于 2020-3-17 21:38
其实,这段代码是错的!!小甲鱼后面应该会有说

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

print("抱歉,输入不合法,", end='')
这个end=是啥意思 是不是前面的教程还没有学到

wuqikt 发表于 2020-3-17 21:41:51

zltzlt 发表于 2020-3-17 21:38
其实,这段代码是错的!!小甲鱼后面应该会有说

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

楼上说的没错

Cute_Traver_Cat 发表于 2020-3-17 21:48:25

1楼说的没错{:10_256:}

hu2008xin 发表于 2020-3-17 21:49:34

zltzlt 发表于 2020-3-17 21:38
其实,这段代码是错的!!小甲鱼后面应该会有说

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

看下我三楼第二个问题呗{:5_109:}

yushshzhh 发表于 2020-3-17 23:34:32

hu2008xin 发表于 2020-3-17 21:49
看下我三楼第二个问题呗

用空格来代替换行,这样就可以继续输入了(在同一行,形式上用户好接受,不然又要另起一行)

zltzlt 发表于 2020-3-18 07:55:24

hu2008xin 发表于 2020-3-17 21:41
这个end=是啥意思 是不是前面的教程还没有学到

请见:https://fishc.com.cn/thread-159045-1-1.html

wangka 发表于 2020-3-18 08:16:34

完整游戏代码1-50(可以猜5次)import random
secret = random.randint(1,50)
print("-------------Guess number game--------------")
temp = input("Guess what number I am thinking in 1-50 (try 5 times) : ")
guess = int(temp)
if guess == secret:
      print("You get it!")
      print("It was it!")
else:
      if guess > secret:
            print("It was bigger than the number")
      else:
            print("It was smaller than the number")
counts = 4
if guess != secret:
      while counts > 0:
                temp = input("Incorrect ,try again: ")
                guess = int(temp)
                if guess == secret:
                        print("You get it!")
                        print("It was it! ")
                else:
                        if guess > secret:
                            print("It was bigger than the number")
                        else:
                            print("It was smaller than the number")
                counts = counts - 1
print("Game Over^_^")

hu2008xin 发表于 2020-3-20 17:57:08

wangka 发表于 2020-3-18 08:16
完整游戏代码1-50(可以猜5次)

谢谢
页: [1]
查看完整版本: 新人提问