您好我希望获得在python中替换故事中的单词的基础知识。 到目前为止,我有以下代码。我正在运行spyder。
def Travsnguard():
print ("Welcome to Travsngaurd!")
#relative = input("Enter a type of relative.")
name = input("Enter a name")
#verb = input("Enter an 'ing' verb" )
#adjective = input("Enter an adjective")
#noun = input("Enter a noun")
print ("Hello,", name, "your journey starts now!")
enter code here
我的程序刚刚运行,没有做任何其他事情。
答案 0 :(得分:1)
这称为字符串连接。在这种情况下,您可以使用“+”符号。
name = input("What is your name?")
print("Hi, " + name + " nice to meet you."
在更复杂的情况下,您可能希望执行字符串插值 - 它采用固定字符串并插入值,如下所示:
name = input("What is your name?")
birthsign = input("What is your birthsign?")
race = input("What is your race?")
father = input("Who was your father?")
#%s = insert string - we then pass the variables to insert in order of use
storyString = "Welcome %s son of %s of the sign %s and race %s. You have travelled far." % (name, father, birthsign, race)
print(storyString)
答案 1 :(得分:0)
我切换到原始输入以确保我们获取字符串然后在结尾处将结果连接成一个字符串。这是你想要做的事情吗?
def Travsnguard():
print ("Welcome to Travsngaurd!")
relative = input("Enter a type of relative: ")
name = input("Enter a name: ")
verb = input("Enter an 'ing' verb: " )
adjective = input("Enter an adjective: ")
noun = input("Enter a noun: ")
print ("Hello,", name, "your journey starts now!")
print ("You are here with your " + adjective + " " + relative + " raul. and you\
are going to go " + verb + ". You are going to use your " + noun + ".")
Travsnguard()