python 005动动手作业0里面的end=" “作用?
直接上作业答案代码import random
times = 3
secret = random.randint(1,10)
print('------------------我爱鱼C工作室------------------')
guess = 0
print("不妨猜一下小甲鱼现在心里想的是哪个数字:", end=" ")
while (guess != secret) and (times > 0):
temp = input()
if temp.isdigit():
guess = int(temp)
if guess == secret:
print("我草,你是小甲鱼心里的蛔虫吗?!")
print("哼,猜中了也没有奖励!")
else:
if guess > secret:
print("哥,大了大了~~~")
else:
print("嘿,小了,小了~~~")
if times > 1:
print("再试一次吧:", end='')
else:
print("机会用光咯T_T")
else:
print("抱歉,您的输入有误,请输入一个整数:", end='')
times = times - 1 # 用户每输入一次,可用机会就-1
print("游戏结束,不玩啦^_^")
之前几节课都没有学过用end=" "
突然出来就有些懵逼了。自己百度了一下end=‘ ’是末尾不换行的意思,但我完全不懂不换行在这什么意思??
还有在while (guess != secret) and (times > 0):
temp = input()
为什么temp = input() 会改到这里,而且()里面可以不写东西吗?那又怎么知道()代表是输入哪里?
一脸?????{:10_266:}{:10_266:}{:10_266:}
有大佬解答一下吗? input()的作用是接受用户输入/接受得到的东西会是个字符串
int()的作用是把接受得到的字符串转换为整数类型,
print()默认打印完字符串会自动添加一个换行符
end 是函数print()的一个参数,用来指定打印结束时末尾的符号,默认是回车/即换行 这是print的官方文档的一部分:
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
将 objects 打印到 file 指定的文本流,以 sep 分隔并在末尾加上 end。 sep, end, file 和 flush 如果存在,它们必须以关键字参数的形式给出。
所有非关键字参数都会被转换为字符串,就像是执行了 str() 一样,并会被写入到流,以 sep 且在末尾加上 end。 sep 和 end 都必须为字符串;它们也可以为 None,这意味着使用默认值。 如果没有给出 objects,则 print() 将只写入 end。
是不是不大好懂,官方文档就这样,严谨得不得了,难得看懂{:10_269:}。简单来说,end是print的一个参数,默认值是'\n',就是换行符,print的时候会加到打印的内容最后,end=''就是说不用换行了,光标会在打印的内容后面而不是下一行。
input就不上官方的了,简单来说,input('内容')相当于print('内容',end=’‘)加上input(),就是在你输入前打印一段字符给你看,input(),没有参数时,就在当前光标出输入。在这段代码里,第一次循环是在"不妨猜一下小甲鱼现在心里想的是哪个数字:"的后面输入,后面的循环是在"再试一次吧:"或者"抱歉,您的输入有误,请输入一个整数:"后面输入。 本帖最后由 cug_cui 于 2020-4-1 01:40 编辑
1.end=" "的问题。
在idle中输入help(print),可以查看print()函数文档。函数有默认参数,其中一个end='\n',表示打印完内容会自动换行。end='' " 表示打印完加一个空格,从而不会换行。
>>> help(print)
Help on built-in function print in module builtins:
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.
2.input问题。
同样查询input。input的括号内可以加提示符(语),也可以不加,默认为空(prompt=None)。
input改在while循环里是为了实现3次机会,猜错了可以再次输入。
input函数的作用是读入一个字符串。
>>> help(input)
Help on built-in function input in module builtins:
input(prompt=None, /)
Read a string from standard input.The trailing newline is stripped.
The prompt string, if given, is printed to standard output without a
trailing newline before reading input.
If the user hits EOF (*nix: Ctrl-D, Windows: Ctrl-Z+Return), raise EOFError.
On *nix systems, readline is used if available.
>>>
3.“那又怎么知道()代表是输入哪里?”这句话不知道想问什么。
end默认的值是"\n" 临时修改 print()内置函数的分隔符为空格
页:
[1]