如何在python中向print函数添加变量?

时间:2017-06-28 19:52:38

标签: python printing

好的,所以我输入了我的代码,但是我一直遇到语法错误。那么,为什么它不起作用?请告诉我。 感谢

# Odd or Even?
print('Hello! What is your name?')
PlayerName = input()
print("Hello,"  +PlayerName "! Enter your number and I\'ll tell you if it\'s even or odd!")
PlayerNum = input()
List = list(PlayerNum)
print(PlayerNum)

3 个答案:

答案 0 :(得分:1)

print("Hello,"  +str(PlayerName) + "! Enter your number and I\'ll tell you if it\'s even or odd!")

print("Hello, {}! Enter your number and I\'ll tell you if it\'s even or odd!".format(PlayerName))

答案 1 :(得分:1)

使用格式化功能

print("hello world {someparam}".format(name='foo')

答案 2 :(得分:0)

试用此代码:

请记住添加" +"在变量和字符串之间签名以打印组合句子。

get_queryset

至于确定它是偶数还是奇数,我不确定你是否已经使用了这个代码,但这是我将使用的:

print("Hello! What is your name?")
playerName = input()
print("Hello, " + playerName + "! Enter your number and I'll tell you if it's even or odd!")
playerNum = input()
List = list(playerNum)
print(playerNum)