print "HEllo !!"
print "Today, I will calculate something for you !!"#introduces itself
inp = True
def start():
print "type 1 for ADDITION"
print "type 2 for SUBTRACTION"
print "type 3 for MULTIPLICATION" #asks for the kind of calculation
print "type 4 for DIVISION"
press=int(raw_input())
if press >= 5 :
print "Choose a NUMBER which is less than 5 and you will get the answer!!"
print "-------------------------------"
if press == 1:
print "Enter your first number"
num1 = input()
print "enter your second number" #adds
num2 = input()
result = num1 + num2
print " Your answer is "+ str(result)
print "-------------------------------"
if press == 2 :
print "Enter your first number"
num1 = input()
print "enter your second number" #subtracts
num2 = input()
result = num1 - num2
print " Your answer is "+ str(result)
print "-------------------------------"
if press == 3:
print "Enter your first number"
num1 = input()
print "enter your second number"
num2 = input() #multiplies
result = num1 * num2
print " Your answer is "+ str(result)
print "-------------------------------"
if press == 4:
print "Enter your first number"
num1 = input() #divides
print "enter your second number"
num2 = input()
result = num1/num2
print " Your answer is "+ str(result)
print "-------------------------------"
while inp :
start()
所以我創建了這個使用基本命令的計算器,唯一的問題是當有人輸入一個字母,它要求從1到4的數字時,它顯示一個python錯誤。相反,我希望能夠打印一個自定義的錯誤,如「只允許數字」或類似的東西,而不是python顯示的常規錯誤。我可以做些什麼改變,以便用數字和字母表示,但是當有人輸入一個字母時會顯示一個定製的錯誤? P.S. :我要求的VAR「新聞」,而不是關於「NUM1」或「NUM2」python:如何接收字母?
P.S. :我要求var「press」而不是「num1」或「num2」,它不是重複的。類似的問題與我所要求的方式太不一樣了。請再次查看並重新打開。 – Ashok