Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 函数生成随机故事返回始终相同的输出_Python - Fatal编程技术网

Python 函数生成随机故事返回始终相同的输出

Python 函数生成随机故事返回始终相同的输出,python,Python,基于这幅漫画,我用Python创建了一个非常简单的故事生成器: 每次我运行脚本时,它都会生成一个新的随机故事,但是,如果用户选择通过写“y”再次运行它,则生成的故事总是相同的。我做错了什么 代码如下: # a random spoiler generator based on this comic: # https://xkcd.com/2243/ import random # define the various options and choose randomly one villa

基于这幅漫画,我用Python创建了一个非常简单的故事生成器:

每次我运行脚本时,它都会生成一个新的随机故事,但是,如果用户选择通过写“y”再次运行它,则生成的故事总是相同的。我做错了什么

代码如下:

# a random spoiler generator based on this comic:
# https://xkcd.com/2243/

import random

# define the various options and choose randomly one
villain = random.choice(['Kyle Ren', 'Malloc', 'Darth Sebelius', 'Theranos', 'Lord Juul'])
friend = random.choice(['Kym Spacemeasurer','Teen Yoda','Dab Tweetdek', 'Yaz Progestin', 'TI-83'])
lightsaber = random.choice(['beige', 'ochre', 'mauve', 'aquamarine', 'taupe'])
superweapon = random.choice(['Sun Obliterator', 'Moonsquisher', 'World Eater', 'Planet Zester', 'Superconducting Supercollider'])
superpower = random.choice(['blowing up a planet with a bunch of beams of energy that combine into one', 'blowing up a bunch of planets with one beam of energy that splits into many', 'cutting a planet in half and smashing the halves together like two cymbals', "increasing the CO2 levels in a planet's atmosphere, causing rapid heating", 'triggering the end credits before the movies is done'])
old_enemy = random.choice(['Boba Fett', 'Salacious Crumb', 'The Space Slug', 'The Bottom Half of Darth Maul', 'YouTube Commenters'])
feat = random.choice(['a bow that shoots little lightsaber-headed arrows.', 'X-Wings and TIE Fighters dodging the giant letters of the opening crawl.', 'a Sith educational display that uses force lightning to demonstrate the dielectric breakdown of air.', 'Kylo Ren putting on another helmet over his smaller one.', 'a Sith car wash where the bristles on the brushes are little lightsabers.'])
father = random.choice(['Luke', 'Leia', 'Han', 'Obi-Wan', 'a random junk-trader'])
mother = random.choice(['Poe.', 'BB-8.', 'Amilyn Holdo.', 'Laura Dern.', 'a random junk-trader.', 'that one droid from the Jawa Sandcrawler that says "gonk".'])

# creates the parts of the story
intro = 'In this Star Wars movie, our heroes return to take on the First Order and new villain '
part_1 = '. With help from their new friend '
part_2 = ', Rey builds a new lightsaber with a '
part_3 = " blade, and they head out to confront the First Order's new weapon, the "
part_4 = ', a space station capable of '
part_5 = '. They unexpectedly join forces with their old enemy, '
part_6 = ', and destroy the superweapon in a battle featuring '
part_7 = "\n\nP.S. Rey's parents are "
part_8 = ' and '

# generates the story
def rsg():
  print(intro + villain + part_1 + friend + part_2 + lightsaber + part_3 + superweapon + part_4 + superpower + part_5 + old_enemy + part_6 + feat + part_7 + father + part_8 + mother)

# asks user to generate another story or not
while True:
  rsg()
  while True:
    user_input = input('Would you like to generate a new spoiler? [y,n]\n')
    if user_input not in ('y', 'n'):
      print('Please enter "y" for yes or "n" for no.')
      continue
    if user_input == 'y':
      break
    else:
      print('Alright, bye!')
      quit()

变量永远不会更新,只是在程序开始时计算。
将所有的
随机。选择
rsg
函数中的
行,您会很好

您使用的是
random.choice
仅在首次启动时使用。应该是:

villains = ['Kyle Ren', 'Malloc', 'Darth Sebelius', 'Theranos', 'Lord Juul']

def rsg():
  print(intro + random.choice(villains) ....

啊,当然,这很有道理,非常感谢您的明确解释!非常感谢您的评论和帮助,现在看来很明显!