So I write a calculator then I tried to write a shorter version and there is one part of the code isn't working as I need:
import os
while True:
cont = input('Do you want to continue? (y for yes or n for no)\n')
os.system('clear') if os.name == 'posix' else os.system('cls') if cont == 'Y' or cont == 'y' else break if cont == 'N' or cont == 'n' else print('That's not y or n!')
And this is the longer version:
import os
while True:
cont = input('Do you want to continue? (y or n)\n')
if cont == 'Y' or cont == 'y':
if os.name == 'posix':
os.system('clear')
else:
os.system('cls')
elif cont == 'N' or cont == 'n':
break
else:
print('That's not y or n!')
Sorry for not commenting anything!
I can't do anything to the break
at there because I've put the code into a while True:
loop. I tried put in the i = 1
then change the while True:
to while i = 1:
and changed the break
to i = 0
but it gives even more errors.
CodePudding user response:
You could easily shrink down your your code and keep it readable:
import os
cont = input('Do you want to continue? (y or n)\n').lower()
if cont == 'y':
cmd = "clear" if os.name == 'posix' else "cls"
os.system(cmd)
elif cont not in ('n', 'y'):
print('That's not y or n!')
else: # if needed
break
CodePudding user response:
In order to add break
, there should be a loop (like for or while). If you want to exit from the program if user input 'n' or 'N', you can do something like this.
elif cont == 'N' or cont == 'n':
exit
Since there is no loop to break, you can simply remove this elif
snippet and handle not inserting 'n' or 'N' or 'y' or 'Y' like this.
elif cont != 'N' or cont != 'n':
print("That's not 'n' or 'y'")