0

basically what I am asking is imagine you made a game in Python and after you once play it and finished it, we ask the user if they would like to continue? if they choose 'yes', is there a way to clear the screen and restart the game from the beginning? (without having the code execution(game) of the previous play of the user)

Just a Small Example - def add(num1,num2): return num1 + num2

def enter_number():
    num1 = int(input("Enter First Number: "))
    num2 = int(input("Enter Second Number: "))

    print(add(num1,num2))

enter_number()

would_continue = input("Would you like to Continue? 'y' or 'n'")

if would_continue == 'y':
    #Way to Clear Screen?
    enter_number()
else:
    print("Goodbye, user :D")
  • A common approach for this is to simply print an excess amount of empty lines, however many lines in height you expect the application to run in at maximum. To actually "clear" the screen back to normal, you'd need some ncurses library in Python. Try looking at https://docs.python.org/3/howto/curses.html – h0r53 Aug 22 '23 at 16:56

1 Answers1

1

Anything wrong with

import os

os.system('clear') # unix
os.system('cls') # windows

?

JustLearning
  • 1,435
  • 1
  • 1
  • 9
  • 1
    thanks so much.....after importing os command to the code it works!!! hope you have a great day :D –  Aug 24 '23 at 10:28
  • Glad to help! Yes, `os` is a built-in library so you can import it with confidence on any machine you run the code. – JustLearning Aug 25 '23 at 02:47