Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
67 views
in Technique[技术] by (71.8m points)

python - Creating a function to print a different question based on the argument given

I'm creating a text game to learn the basics of python, it is basically a short quiz game to determine your game character.

I wanted the question to print as if they were being typed, like this:

In=list("What is your name? ")
for x in In:
    print(x, end='')
    stdout.flush()
    time.sleep(0.1)

print("
")

Then I wondered if I could make a function that could print the correct question from a variable containing a list of questions based on the argument given to the function. If the argument given is "1" it would print question 1 and so on... I keep having to come back to functions, I find them one of the most confusing.

I tried the below code, thinking perhaps lists in lists, but then it prints both questions. I need a way to identify just one 'item' from the variable.

import time
from sys import stdout

def ask_question():
    for list in list_questions:
        for item in list:
            for x in item:
                print(x, end='')
                stdout.flush()
                time.sleep(0.1)


list_questions = [["What is your name?"], ["What is your age? "]]

Questions I am wondering (and will look into after sleeping on it, but thought I would put here anyway):

Is it possible to use something other than a list here? Strings are iterable, would they be in some way easier to use for this?

How do you identify a list within a list? Is the below format for strings?

print(list_questions[0,1])

Could a dictionary be used?

Is there a completely different, better way of doing this whole thing?

If anyone has an abundance of free time and wat to look over the whole text game (there are many, many problems to be found) feel free:

https://github.com/hyperblue1356/Radiant_text_game/blob/main/code

question from:https://stackoverflow.com/questions/65904441/creating-a-function-to-print-a-different-question-based-on-the-argument-given

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can create a dict, containing all the questions that are linked to the variables.

questionsdict = {1: "What is your name?", 2: "What is your age?"}

You can then iterate over the characters to create the typing effect that you envision.

for char in questionsdict[question_no]:
    print(char, end="")
    time.sleep(0.1)

To wrap it all in a function, you need a function that accepts the value that relates to a question.

import time

def ask_question(question_no):
    questionsdict = {1: "What is your name?", 2: "What is your age?"}
    for char in questionsdict[question_no]:
        print(char, end="")
        time.sleep(0.1)

You can then call ask_question(1) or ask_question(2) to get either of the two questions printed out.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...