gpt4 book ai didi

python - 如何用 python 创建可编辑的故事?

转载 作者:太空宇宙 更新时间:2023-11-03 15:11:23 26 4
gpt4 key购买 nike

你好,我正在寻找用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

我的程序只是运行,不执行任何其他操作。

最佳答案

这称为字符串连接。在这种情况下,您可以只使用“+”符号。

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)

关于python - 如何用 python 创建可编辑的故事?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44192252/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com