python - Python:TypeError:+的不支持的操作数类型:'Random'和'str'
问题描述
我正在输入一个随机生成单词的代码。但它说 TypeError 并且随机不能与字符串匹配
我曾多次尝试重写代码,但没有成功。代码:
import random
from random import *
Letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
LettersInWord = Random()
Points = 0
print(LettersInWord + " is number of letters in word")
for index in range(LettersInWord):
Word = Letters[Random.random(0, 25)]
Guess = input("Enter Word You Think It Is (You will get a point everytime
one or more of your letters match with the word. Your turn will end when
you enter a letter that is not in the word: ")
for letter in Guess:
if letter == letter in Word:
Points = Points + 1
if Guess == Word:
print("Congratulations, you won. End program and restart to
try again.")
Guess = input("Well done, try again")
解决方案
Random()
是一个随机数生成器,而不是一个数字本身。
letters_in_word = randint(1,25) # random number between 1 and 25, inclusive
(大写名称通常保留给类名。对普通变量使用蛇形大小写标识符。)
推荐阅读
- python - 我想知道如何将带有值列表的字典映射到数据框
- spring-boot - 尝试使用 Tomcat 在 Docker 中将 Springboot 应用程序作为 war 文件运行时出现问题
- sbt - SBT 1.5.5 在使用 -mem 选项运行测试时出错
- python - pip:没有为检测器2找到匹配的分布
- nginx - 将 2 个 url (%20) 重定向到 nginx 中的一个
- angular - ChartJs:删除两点之间的线
- r - 仅选择具有子组且都具有观察值的组
- ios - 如何自定义 iOS/tvOS 中提供的 PiP 的 UI?
- sql - Postgres 转置一行
- azure - 将 Azure IoT 数据发送到 azure gen2