Write exception handling statements in Python

CSDN Q & A 2022-05-14 15:46:14 阅读数:624

writeexceptionhandlingstatementspython

img

import random
n=int(input(' Please enter the lower limit :'))
m=int(input(' Please enter the upper limit :'))
num=random.randint(n,m)
i=0
while(i<5):
a=int(input(" Please enter a number :"))
How to handle the input exception statement




Take the answer :

Is to use try except Handle exception statements
The answer code of your question is as follows :

import randomn=int(input(' Please enter the lower limit :'))m=int(input(' Please enter the upper limit :'))num=random.randint(n,m)i=0while(i<5): try: a=int(input(" Please enter a number :")) except ValueError: print(' It's not a number , Please re-enter ') continue i+=1 if a<num: print(' Guess a little ') elif a>num: print(' Guess the ') else: print(' Congratulations on your guesses , The number is {}'.format(a)) breakif i==5: print(" More than 5 Time , Failure ")

If help , Please click... Below my answer 【 Adopt the answer 】 Button to help adopt , thank you !

img


版权声明:本文为[CSDN Q & A]所创,转载请带上原文链接,感谢。 https://pythonmana.com/2022/134/202205072343190791.html