当前位置: 首页 > 面试题库 >

随机词生成器-Python

徐绪
2023-03-14
问题内容

所以我基本上是在一个项目中,计算机从单词列表中提取一个单词,然后为用户弄乱它。只有一个问题:我不想一直在列表中写很多单词,所以我想知道是否有一种方法可以导入很多随机单词,所以即使我也不知道它是什么,并且那我也可以玩游戏吗?这是整个程序的编码,我只输入了6个字:

import random

WORDS = ("python", "jumble", "easy", "difficult", "answer",  "xylophone")
word = random.choice(WORDS)
correct = word
jumble = ""
while word:
    position = random.randrange(len(word))
    jumble += word[position]
    word = word[:position] + word[(position + 1):]
print(
"""
      Welcome to WORD JUMBLE!!!

      Unscramble the leters to make a word.
      (press the enter key at prompt to quit)
      """
      )
print("The jumble is:", jumble)
guess = input("Your guess: ")
while guess != correct and guess != "":
    print("Sorry, that's not it")
    guess = input("Your guess: ")
if guess == correct:
    print("That's it, you guessed it!\n")
print("Thanks for playing")

input("\n\nPress the enter key to exit")

问题答案:

如果您重复执行此操作,我将在本地下载它并从本地文件中提取。* nix用户可以使用/usr/share/dict/words

例:

word_file = "/usr/share/dict/words"
WORDS = open(word_file).read().splitlines()

远程字典中提取

如果您想从远程词典中提取信息,可以采用以下两种方法。请求库使这变得非常容易(您必须pip install requests):

import requests

word_site = "https://www.mit.edu/~ecprice/wordlist.10000"

response = requests.get(word_site)
WORDS = response.content.splitlines()

或者,您可以使用内置的urllib2。

import urllib2

word_site = "https://www.mit.edu/~ecprice/wordlist.10000"

response = urllib2.urlopen(word_site)
txt = response.read()
WORDS = txt.splitlines()


 类似资料:
  • 问题内容: 鉴于此功能,我想更换 颜色 与颜色随机发生器。 我该怎么做? 问题答案: 使用代替:

  • 生成随机数 # random_random.py import random for i in range(5): print('%04.3f' % random.random(), end=' ') print() # random_uniform.py import random for i in range(5): print('{:04.3f}'.format(ran

  • random 生成随机数包 文档:https://www.npmjs.com/package/random 安装:npm install --save random 封装代码: app / extend / context.js // 导入 jwt const jwt = require('jsonwebtoken') // 导入随机数包 const random = require('rando

  • 问题 你需要生成在一定范围内的随机数。 解决方案 使用 JavaScript 的 Math.random() 来获得浮点数,满足 0<=X<1.0 。使用乘法和 Math.floor 得到在一定范围内的数字。 probability = Math.random() 0.0 <= probability < 1.0 # => true # 注意百分位数不会达到 100。从 0 到 100 的范围实

  • Python3 实例 以下实例演示了如何生成一个随机数:# -*- coding: UTF-8 -*- # Filename : test.py # author by : www.runoob.com # 生成 0 ~ 9 之间的随机数 # 导入 random(随机数) 模块 import random print(random.randint(0,9)) 执行以上代码输出结果为: 4 以上