2021年1月4日星期一

why can't my imported words run in python?

import random from words import words import string

def get_valid_word(words): word = random.choice(words) #randomly chooses something from that list while '-' in word or ' ' in word: word = random.choice(words)

    return word        def hangman():          word = get_valid_word(words)          word_letters = set(word) #letters in the word          alphabet = set(string.ascii_uppercase)          used_letters = set() #what the user has guesssed                # getting user input          while len(word_letters) > 0:              # letters used              # ' '.join(['a', 'b', 'c']) --> 'a b cd'              print('you have used these letters:', ' '.join(used_letters))                # what current word is (ie w - R D)              word_list = [letter if letter in used_letters else '-' for letter in word]              print('current word: ', ' '.join(word_list))                            user_letter = input('Guess a letter: ')          if user_letter in alphabet - used_letters:              used_letters.add(user_letter)              if user_letter in word_letters:                  word_letters.remove(user_letter)            elif user_letter in used_letters:              print(' you have already used those charater. please try again.')            else:                  print('Invalid character. please try again.')                                    # gets here when len(word_letters) == 0  
https://stackoverflow.com/questions/65571907/why-cant-my-imported-words-run-in-python January 05, 2021 at 09:06AM

没有评论:

发表评论