简体   繁体   中英

Clearing the Screen in Python Shell

我对这个程序以及如何清除屏幕有点困惑

When executing this code:

wordsToGuess.append(random.choice(wordList))
wordList.remove(random.choice(wordList))

what you're doing is adding a random word to wordsToGuess and removing another random word (different from the first one) from your list.

The simplest solution to your problem is to use random.sample(population,k) to select elements from your list without replacement. You can take a look at the documentation for it here .

Now it looks like this:

def easy():
    wordList = ["dog", "house", "food", "ice", "rich", "school", "team", "walk", "mice", "car", "airplane", "internet", "big", "red",
            "snow","wow", "lose", "run", "line", "duck", "bike", "land", "hill", "bad", "simple", "clock", "virus", "game", "sign", "end"]
    print()
    print("Okay! Welcome to the easiest mode.")
    time.sleep(2)
    print("You will be given five words and must try to remember them in order:")
    time.sleep(3)
    print("Ready")
    time.sleep(1)
    print("Set")
    time.sleep(1)
    print("REMEMBER!")
    time.sleep(0.5)
    wordsToGuess = random.sample(wordList, 5)
    print(wordsToGuess)
    time.sleep(4)
    print(("\n") * 100)
    print("Let's see how good your memory really is")
    return wordsToGuess

wordList.remove(random.choice(wordList)) -> I assume this line is meant to remove the word that was chosen at random from the line above it so it is not repeated, but really you are just telling the program to remove a random element from the list, independent of the one that was previously chosen.

word = random.choice(wordList)
wordsToGuess.append(word)
wordList.remove(word)

This will "flash" the values on the screen and not print them all together in a list. I also added a 1/2 second delay between the prints to look better.

import time
import random

def easy():
    word_list = ["dog", "house", "food", "ice", "rich", "school", "team", "walk", "mice", "car", "airplane", "internet", "big", "red",
                 "snow","wow", "lose", "run", "line", "duck", "bike", "land", "hill", "bad", "simple", "clock", "virus", "game", "sign", "end"]
    print()
    print("Okay! Welcome to the easiest mode.")
    time.sleep(2)
    print("You will be given five words and must try to remember them in order:")
    time.sleep(3)
    print("Ready")
    time.sleep(1)
    print("Set")
    time.sleep(1)
    print("REMEMBER!")
    time.sleep(0.5)

    words_to_guess = []
    for i in range(5):
        word = random.choice(word_list)
        words_to_guess.append(word)
        time.sleep(0.5)
        word_list.remove(word)
        print(words_to_guess[i])
    time.sleep(4)
    print(("\n") * 100)
    print("Let's see how good your memory really is")

    return words_to_guess

I didn't see the rest of the code though. i am guessing easy is the gamemode, but why end it so fast? You could maybe instead of returning the words_to_guess list, have the user input words and give him some time to type, probably in a thread or something.

Not an answer, more like a suggestion:

import time
import random
import threading

def counter(time):
    time.sleep(time)

def get_input(words: int, time: float):
    word_list = []
    print("You will have {} seconds to write all the words you remember!".format(time))
    time.sleep(2)
    print("Start!")
    countdown = threading.Thread(target=counter, args=time)
    countdown.start()

    while countdown.isAlive():
        if words > 0:
            word = input("Write the words!\n")
            word_list.append(word)
            words -= 1
        else:
            break
    return word_list


def easy():
    word_list = ["dog", "house", "food", "ice", "rich", "school", "team", "walk", "mice", "car", "airplane", "internet", "big", "red",
                 "snow","wow", "lose", "run", "line", "duck", "bike", "land", "hill", "bad", "simple", "clock", "virus", "game", "sign", "end"]
    print()
    print("Okay! Welcome to the easiest mode.")
    time.sleep(2)
    print("You will be given five words and must try to remember them in order:")
    time.sleep(3)
    print("Ready")
    time.sleep(1)
    print("Set")
    time.sleep(1)
    print("REMEMBER!")
    time.sleep(0.5)

    words_to_guess = []
    for i in range(5):
        word = random.choice(word_list)
        words_to_guess.append(word)
        time.sleep(0.5)
        word_list.remove(word)
        print(words_to_guess[i])
    time.sleep(4)
    print(("\n") * 100)
    print("Let's see how good your memory really is")

    words = get_input(5, 10)  # this will give the user 10 seconds to write 5 words
    wrongs = []
    for i in range(words):
        if words[i] != words_to_guess[i]:
            wrongs.append(i)
    if wrongs:
        print("You made {} mistakes:\n".format(len(wrongs)))
        for i in range(wrongs):
            print("{}) You wrote '{}'. The word was ''".format(i, wrongs[i], words_to_guess[i]))
    else:
        print("Nice! You made no mistakes!")

I didn't run it, but it might work :P

By the way the best practice in python is to use snake case ( snake_case ) for variable names and function names. You can use camel case ( camelCase ) but only for class names

I think you are using this function in a loop or something that repeats the function, if you are then you need to move the list out of function. because every time you call it, it makes the same list over and over.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM