[英]I cannot output print quotation text and out at the same time
对于我的学校项目,我正在开发一个播放列表生成器,但我卡住了,请在此处查看此代码?
elif SuggestionMetal == "n":
SongsMetal = input("Please can you input what songs you want in your playlist: ")
print ("You have chosen" (SongsMetal))
说“打印(“你选择了”(SongsMetal))”的部分不起作用,我不知道为什么。 这是错误:
回溯(最近一次调用):文件“D:\\ComputingProjectUpdated.py”,第 21 行,打印中(“你选择了”(SongsMetal))类型错误:“str”对象不可调用
有人可以帮帮我吗? 谢谢。 我正在使用 Python 3.6.2 顺便说一句。
使用Python>=3.6
请使用.format()
或 f-string 。
print('You have chosen {}'.format(SongsMetal)) # the {} is a place holder, used to add any variable.
还要注意python变量应该是小写的并用下划线分隔; 例如:
songs_metal
而不是SongsMetal
。 后者在 Java 中经常使用。 请参阅python 变量命名约定。
说“打印(“你选择了”(SongsMetal))”的部分不起作用,我不知道为什么。
这是因为您正在尝试调用字符串,正如您的错误所述。 这意味着什么? 如果你想在 python 中调用一些东西(例如一个函数),你可以说:
my_function(my_input)
其中python将“调用”my_function,并将my_input作为输入。
在您的情况下,您不是调用函数,而是调用字符串“您已选择”。 它尝试调用字符串的原因是因为您在尝试附加 SongsMetal 的字符串后面有 (),它正在读取 SongsMetal 作为您函数(字符串)“您已选择”的输入。 所以在这种情况下,my_function 是你的字符串,“你选择了”,而 my_input 是你的输入 SongsMetal,它被解释为
"You have chosen"(SongsMetal)
如果去掉不必要的 (),实际上会得到不同的错误(语法)。
SongsMetal = input("Please can you input what songs you want in your playlist: ")
print ("You have chosen" SongsMetal)
^
SyntaxError: invalid syntax
所以你可以通过简单地在你的字符串后面加上一个 + 来解决这个错误
print ("You have chosen" + " " + SongsMetal)
或者简单地打印 2 个项目:
print ("You have chosen" , SongsMetal)
您还可以编写一个函数,然后在打印语句中调用它,如下所示:
def my_function():
SongsMetal = input("Please can you input what songs you want in your playlist: ")
mytext = "You have chosen"
return " ".join([mytext, SongsMetal])
print (my_function())
这最后一个函数是 python 试图解释您的代码的内容,因为您在字符串后添加了 (),它试图将该字符串作为函数/类读取,例如“somestring”(),这将导致您的错误.
print ("You have chosen", (SongsMetal))
那样会很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.