I am currently writing a code that accepts user inputs. However, I intend to put in an option such that if the user made an error, they may restart the process again by typing in a specific input. And I’m hoping that the code clears all previous input from the namespace:
For example
JavaScript
x
8
1
name = input("what is your name: ")
2
age = input("how old are you:? ")
3
## if the user realizes that they put in the wrong name and wish to restart the process, they may input "restart"
4
if age == 'restart':
5
### I don't know what code to put here.
6
else:
7
#I'd continue the rest of my codes
8
I’d greatly appreciate any tips.
Advertisement
Answer
Supposedly you want the console to clear if the user decides to change its input in name
JavaScript
1
14
14
1
import os
2
3
while True:
4
os.system('clear') #we imported "os" for this built-in function
5
name = input("Enter your name: ")
6
age = input("how old are you:? ")
7
8
if age == 'restart': #if user inputs 'restart' we head back to the start of the loop
9
input("Press Enter Key to Continue...")
10
continue
11
12
# Rest of the code goes here
13
14