首页 > 解决方案 > 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")

标签: python

解决方案


Random()是一个随机数生成器,而不是一个数字本身。

letters_in_word = randint(1,25)  # random number between 1 and 25, inclusive

(大写名称通常保留给类名。对普通变量使用蛇形大小写标识符。)


推荐阅读