+ 1
want to terminate the script if input is anything other than ok,my script just skips to 2question if input is not "ok" on line3
#True-False Quiz print('Lets play a (-: <<True-False>> :-) Quiz \n') print('Answer the questions by selecting correct options \n') x=input('READY to PLAY? Please Type, ok \n') if x=='ok': print('1. Who was the first PM of India? \n') print('A. Jawahar lal Nehru \n') print('B. Lal Bahadur Shastri \n') print('C. Morarji Desai \n') print('D. Mahatma Gandhi \n') x=input('type you answer= \n') if x=='a': print('TRUE, GOOD! \n') else: print('FALSE!!! \n') print('2.question ?')
3 Respuestas
+ 5
watch out the indentation:
one way you can do it is:
play = input("READY to PLAY? ")
if play == 'ok':
# handle all the questions and play logic here (indent the code so it is inside the if).
...
# if the user did not enter ok the program jumps to here.
+ 3
Give one more indent space to everything below and including the "if x=='a':", also including the "print('2.question ?)" statement. This will make it part of the firsf "if" statement and will be skipped when x is different to 'ok'.
+ 2
Another solution >>> indicates a new line.
#True-False Quiz
>>>import sys
print('Lets play a (-: <<True-False>> :-) Quiz \n')
print('Answer the questions by selecting correct options \n')
x=input('READY to PLAY? Please Type, ok \n')
if x=='ok':
print('1. Who was the first PM of India? \n')
print('A. Jawahar lal Nehru \n')
print('B. Lal Bahadur Shastri \n')
print('C. Morarji Desai \n')
print('D. Mahatma Gandhi \n')
x=input('type you answer= \n')
>>>else:
>>> print("Error Message (optional line)")
>>> sys.exit()
if x=='a':
print('TRUE, GOOD! \n')
else:
print('FALSE!!! \n')
print('2.question ?')
sys.exit() Will exit the program, preventing further code execution.