QuesAns Print Page – CSKC
Sign In

Search

siteicon
Q&A CBSE12D-2016-B02B #6466

Question:

What will be the output of the following python code considering the following set of inputs?
JAYA
My 3 books
PICK2
2120
Also, explain the try and except used in the code.

Counter=0
while True:
    try:
        Number=int(raw_input("Give a Number"))
        break
    except ValueError:
        Counter=Counter+2
        print("Reenter Number")
        print(Counter)

Answer:

Output:
Give a Number JAYA
Re-enter Number
Give a Number My 3 books
Re-enter Number
Give a Number PICK2
Re-enter Number
Give a Number 2120
6

Explanation: The code inside try makes sure that the valid number is entered by
the user. When any input other than an integer is entered, a value error is thrown
and it prompts the user to enter another value.