2

I am a beginner, and I am putting together a Christmas game for a family member. In the beginning, the game asks the player for a particular input, let's say the number of wine bottles in the cellar.

Hence, I have written something like this:

correct_wine = 8
wine = int(input("How many wine bottles are there? "))
while correct_wine != wine:
   print("Wrong answer. Please try again.")
   wine = int(input("Number of bottles: "))

At the very end, the game asks the player for the same input.

new_wine = int(input("Just to make sure, how many wine bottles were there? "))
while correct_wine != new_wine:
   print("Perhaps you should go and count them again.")
   new_wine = int(input("Number of bottles: "))

Obviously, I hope that the player has forgotten the number of bottles by the end of the game and has to count them again. However, while the first input remains visible on the screen, it won't be difficult for the player to copy it, thus destroying the point of asking the question again. Is there a way to make the first input disappear?

2 Answers 2

1

You could try calling cls or clear depending on if the users operating system is Windows or not e.g., Darwin(macOS), Linux, etc.:

from platform import system
from subprocess import call

def get_int_input(prompt: str) -> int:
    while True:
        try:
            return int(input(prompt))
        except ValueError:
            print("Error: Enter an integer, try again...")

correct_wine = 8
wine = get_int_input("How many wine bottles are there? ")
while correct_wine != wine:
    print("Wrong answer. Please try again.")
    wine = get_int_input("Number of bottles: ")
print("Correct")

# ... Other parts of your quiz go here

call('cls') if system() == 'Windows' else call('clear')

new_wine = get_int_input("Just to make sure, how many wine bottles were there? ")
while correct_wine != new_wine:
   print("Perhaps you should go and count them again.")
   new_wine = get_int_input("Number of bottles: ")
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! The operating system, where I intended to run the code, is macOS, so I assume I'll have to change the "Windows" call in the code to "Darwin", right?
It should work as is. system() will return 'Darwin' so the else condition will be run. i.e., call('clear') since 'Darwin' != 'Windows'. For more info see: book.pythontips.com/en/latest/ternary_operators.html
0

The player will write the input on the console, which your Python program has no control over, so no if you use the function input you cannot do it. You can do it with a GUI though or curses where your programm controlls the screen.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.