2017-08-27 63 views
-4

我在学习Python,并且拼凑了一个随机的句子生成器来练习。我如何保留随后部分的第一个随机选择的名称?在Python中保留随机名

我有这样的:

def random_name(): 
    line_num = 0 
    selected_line = '' 
    with open('names.txt') as f: 
     while 1: 
      line = f.readline() 
      if not line: break 
      line_num += 1 
      if random.uniform(0, line_num) < 1: 
      selected_line = line 
    return selected_line.strip() 

重复动词和名词。然后我定义了一句:

def MakeSentence(): #makes sentence by combining name, verb, and noun 

x = random_name() + " is " + random_verb() + " " + random_noun() 
return x 

print("Type Yes to continue.") 
print("\n") 
print("Enter 'Q' to quit.") 
print("\n") 
response = 'x' #generic response as to not cause problems 

while (response != 'Q'): #case switch kills it all 

response = input("Would you like to continue?\n") 
if response == 'Q': 
    break 
elif response == 'Yes': 
    print(MakeSentence()) 

response = input("Is " + random_name() + " correct?\n") 
if response == 'Q': 
    break 
elif response == 'Yes': 
    print(random_name() + " is correct.\n") 
+6

你需要知道,很多/我们大多数人都不愿意通读,更不用说调试这么多的代码了。您需要询问具有明确答案的简短的具体编程问题。您应该阅读帮助文件。这并不是说以任何方式阻止你,只是告诉你什么是预期的。欢迎来到SO! –

+0

你一直在反复调用'random_name'。你不能只调用一次并将结果存储在一个变量中吗? – Carcigenicate

+0

谢谢你们两位。我会尽量在将来更简洁。谢谢,Carcigenicate,我想你是对的:) – afilmforthefuture

回答

0

的MakeSentence():完成一次击中return语句,让你有执行之后貌似再重复几个print语句?

可以使用这个返回一个列表或元组:

def MakeSentence(): #makes sentence by combining name, verb, and noun 
    """ 
    (None)->List 
    Returns a list with a random name, verb and noun 
    """ 
    x = [random_name(), random_verb(), random_noun()] 
    return x 

可以做出许多的这些,但是例如,试试这个:

sample = MakeSentence() 

而且在以上格式打印出来:

print(sample[0] + " is " + sample[1] + " " + sample[2]) 

希望这会解决它为您服务!

我如何可以存储第一随机名称,动词和名词在 进一步的陈述中使用?

运行后,您可以看到解释器中发生了什么:该函数返回一个包含三个字符串的列表,并且可以使用括号[0],[1]和[2]来访问如下所示。这三件事直到你或直到你写完为止。

>>> sample = MakeSentence() 
>>> print(sample) 
['Sean', 'eating', 'apples'] 
>>> print(sample[0] + " is " + sample[1] + " " + sample[2]) 
Sean is eating apples 

如果我想获得许多样品,我可以这样做:

many_samples = [] # make a list to store many lists 
for users in range(3): # to generate three samples: change to larger number if desired 
    many_samples.append(MakeSentence()) 

嵌套列表的一个例子可能是这样的(但这是重新排列,使其更易于阅读) :

>>> print(many_samples) 
[ 
    ['Sean', 'eating', 'apples'], 
    ['Jane', 'drinking', 'oranges'], 
    ['Jim', 'chopping', 'chairs'] 
] 

# to print out these in a format as you had in the example: 
for each_elem in many_samples: 
    print(each_elem[0] + " is " + each_elem[1] + " " + each_elem[2]) 

所以我们遍历列表,列表中的每个元素都是另一个列表。 在每个子列表中,我们取第一个(位置0),第二个和第三个(位置2)元素,并将它们插入到我们的字符串输出中。

+0

谢谢。我试过这个,但是当我添加[0]时,它只是打印名称的第一个字母。我究竟做错了什么? 例如response = input(“Is”+ sample_name [0] +“correct?\ n”) 给出:K正确吗? – afilmforthefuture

+0

我的目标是展示“我如何存储第一个随机名称,动词和名词用于进一步的陈述?”如果您只是调用函数random_name()来生成一个名称,那么只返回一个字符串。访问字符串中的字符与访问列表中的元素类似,因此如果将字符串“Mike”分配给名为“name”的变量并输出名称[0],则实际上只会获得第一个字符。我可以添加其他评论或更多的细节,或者如果你需要的话可以添加其他的例子我已经在上面添加了一些细节和评论。 :) – srattigan

+0

谢谢,srattigan。你一直很有帮助:) – afilmforthefuture