2017-05-26 52 views
1

你好,我正在寻找取代Python中的故事中的单词的基础知识。 到目前为止我有下面的代码。我正在运行Spyder。如何在python中创建可编辑的故事?

收集的名字从用户

故事用户

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

真的很愚蠢的问题,但您确实在代码的末尾调用了'Travsnguard()',对吧? – user3080953

回答

0

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

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) 
+0

哇,你们很快就回复谢谢你的帮助! – Limitlessmatrix

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() 
+0

'raw_input'没有在python 3中定义(注意这个问题被标记为python 3.6) – user3080953

+0

那真是令人尴尬,谢谢。 –

+0

这帮助了很多,但我认为我可以在3.6上使用raw_input()函数,它只允许使用input()函数。我确实现在看起来好多了。 – Limitlessmatrix