Python 3:如何将输入和字符串添加到同一行?

发布于 2025-01-11 22:28:34 字数 275 浏览 1 评论 0原文

我必须将输入与字符串放在同一行,但不知道如何操作。 这是代码:

print('Hello! My name is Awesome Computer.')
print('What is your name?')
name = input()
print('It is good to meet you ' + name + '.')
print('How are you today?')
input()
print('Okay.')
print('I am doing great!')

I have to put the input on the same line as the string and can't figure out how.
Here's the code:

print('Hello! My name is Awesome Computer.')
print('What is your name?')
name = input()
print('It is good to meet you ' + name + '.')
print('How are you today?')
input()
print('Okay.')
print('I am doing great!')

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

赏烟花じ飞满天 2025-01-18 22:28:34

函数 input() 接受一个要打印的字符串,因此您可以执行以下操作:

name = input("What is your name? ")

它将在接受输入之前打印该字符串,而不添加换行符

The function input() takes in a string to print so you can do this:

name = input("What is your name? ")

And it will print the string before taking input without adding a newline

找回味觉 2025-01-18 22:28:34

您可以像下面一样将一个字符串放入输入函数参数中,它将打印该字符串并在同一行获取输入。此外,您的第二个输入函数调用没有保存到变量中。

print('Hello! My name is Awesome Computer.')
name = input('What is your name? ')
print('It is good to meet you ' + name + '.')

# don't forget to save your input into a variable 
mood = input('How are you today? ')
print('Okay.')
print('I am doing great!')

You can put a string into the input function parameters like I did below and it will print the string and get the input on the same line. Also, your second input function call wasn't being saved into a variable.

print('Hello! My name is Awesome Computer.')
name = input('What is your name? ')
print('It is good to meet you ' + name + '.')

# don't forget to save your input into a variable 
mood = input('How are you today? ')
print('Okay.')
print('I am doing great!')
束缚m 2025-01-18 22:28:34
print('Hello! My name is Awesome Computer.')
# print('What is your name?')
name = input('What is your name? ')
print('It is good to meet you ' + name + '.')
# print('How are you today?')
input('How are you today? ')
print('Okay.')
print('I am doing great!')

测试结果

Hello! My name is Awesome Computer.
What is your name? John Doe
It is good to meet you John Doe.
How are you today? Healthy
Okay.
I am doing great!
print('Hello! My name is Awesome Computer.')
# print('What is your name?')
name = input('What is your name? ')
print('It is good to meet you ' + name + '.')
# print('How are you today?')
input('How are you today? ')
print('Okay.')
print('I am doing great!')

Test Results

Hello! My name is Awesome Computer.
What is your name? John Doe
It is good to meet you John Doe.
How are you today? Healthy
Okay.
I am doing great!
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文