Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Zyphon_22

Coder
So im working on a text game in python and I first decided to use the time module:

[CODE lang="python" title="original"]import time

print("Hello")
time.sleep(2)
print("How are you")[/CODE]

But now i am creating a function and putting it in place of the time module like this:
[CODE lang="python" title="new and improved"]def dialog():
dialog = input("")

print("Hi")
dialog()
print("How are you")[/CODE]

im in the early stages of development and before i start getting quite far on.
I was wondering whether there is a cleaner version of new and improved which does what i am trying to show.

Thanks
 
Hi Zyphon_22,

I do not understand what you want to achieve... The first and second examples do not produce the same result.

The second example simply inputs text from the console and discards it (It stores it inside a local variable that will be destroyed at the end of the function call).
The following is equivalent:
Python:
def dialog():
    input("")

print("Hi")
dialog()
print("How are you")
Actually, at that point, you could simply write that:
Python:
print("Hi")
input("")
print("How are you")
 
ah okay thank you, what i was trying to say, is whether there is a module that waits for a specific user input.
not just waiting for any user input and then carrying on

This is a rough example and i know the code will not work:

[CODE lang="python" highlight="3,5"]print("Hi")
input("")
if input != "key_enter":
print("please press enter")
elif input == "key_enter":
print("How are you")[/CODE]

that is what i was wondering whether there is something that waits for a user input like enter but ignores anyother input
 
Last edited:
ah okay thank you, what i was trying to say, is whether there is a module that waits for a specific user input.
not just waiting for any user input and then carrying on

[/CODE]

that is what i was wondering whether there is something that waits for a user input like enter but ignores anyother input

Hey, here's something you can do...
Python:
def dialog(text = ""):
    dialog = input(text)
    return dialog

print("Hi")
entry = False
while entry is False:
    entry = dialog() 
    # or you could do like dialog("What is the question?")
    if entry != "1":
        entry = False

anotherOne = dialog("What color is the sky?\n")

In the code above, the script will only continue if they type in 1, otherwise it will just start a new line and expect them to keep trying.
I also went ahead and modified your dialog() function to accept a string as an argument for what should be printed out. The default text is just "".

There are ways to clear out a line in the console, but it is different per Python version / platform (windows,linux,mac)... or some people do like print("\n" * 100) to auto-scroll the user down with 100 blank lines (I am not a fan of this at all).

Your best bet in my opinion is to do something like my code above, and maybe do a print of text if they get correct input, or incorrect.
 

New Threads

Buy us a coffee!

Back
Top Bottom