wgz890813 发表于 2018-1-30 14:37:41

print(end=‘’)

在小甲鱼第九讲习题答案中,print('密码中不能含有"*"号!您还有', count, '次机会!', end=' ')
为什么加了end=' ',就能返回到‘请输入密码:’呢?
count = 3
password = 'FishC.com'

while count:
    passwd = input('请输入密码:')
    if passwd == password:
      print('密码正确,进入程序......')
      break
    elif '*' in passwd:
      print('密码中不能含有"*"号!您还有', count, '次机会!', end=' ')
      continue
    else:
      print('密码输入错误!您还有', count-1, '次机会!', end=' ')   
    count -= 1

sky 发表于 2018-1-30 14:41:51

print函数默认输入打印会换行 end参数默认是\n换行 指定为空字符串就不会换行了
回到继续输入密码跟print无关
是因为count没有满足跳出while循环的条件(此处为小于等于0)

orino 发表于 2018-1-30 14:42:03

end=' '表示以空格结尾,不换行。因为默认print是以换行结尾的,所以指定不换行后,就保持在原来那一行了

BngThea 发表于 2018-1-30 16:44:08

可以进行拼接操作,举个例子
print("goo",end='')
print("d day!")

supermaker 发表于 2018-1-30 19:11:08

print作为内置函数,默认end参数为回车,改为' '后输出结果就不会自动回车了
print(...)
    print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
   
    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file:a file-like object (stream); defaults to the current sys.stdout.
    sep:   string inserted between values, default a space.
    end:   string appended after the last value, default a newline.
    flush: whether to forcibly flush the stream.

1990黑豆 发表于 2018-1-30 19:34:20

内置函数的问题。

wgz890813 发表于 2018-1-31 02:33:07

谢谢,各位鱼油 明白了!!!
页: [1]
查看完整版本: print(end=‘’)