简体   繁体   English

为什么 input() function 返回无?

[英]Why is the input() function returning None?

So I am using Jupyter notebook and when I run the code所以我正在使用 Jupyter 笔记本,当我运行代码时

name = input(print('What is your name?'))

The code will produce the output:该代码将生成 output:

What is your name?

None []

Since I cant copy and paste the rectangle that comes when using the function input(), lets asume the brackets represent that rectangle.由于我无法复制和粘贴使用 function 输入()时出现的矩形,因此假设括号表示该矩形。 Then say you write John Doe in the box, it will then show NoneJohn Doe Why is the word None shown next to the name and the box?然后说你在方框里写John Doe ,它会显示NoneJohn Doe为什么在名字和方框旁边显示None这个词? How do you remove it?你如何删除它? Thanks谢谢

I believe you want to prompt the user to type his/her name and then print out the name?我相信你想提示用户输入他/她的名字然后打印出名字? if so, try this:如果是这样,试试这个:

name = input('What is your name?')
print('Your name is '+ name)

Also with this in mind.也考虑到这一点。 Input takes a prompt string as its argument, which it will print automatically, but print returns None; Input 以一个提示字符串作为参数,它会自动打印,但 print 返回 None; it is this that gets printed by input.这是通过输入打印的。 Your code is equivalent to:您的代码相当于:

name = print(...) # prompt == None
ans = str(input(name))

Instead, use str.format to build the prompt and pass it straight to input相反,使用 str.format 构建提示并将其直接传递给输入

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM