else语句捕获到无效输入后,即使该输入应为有效输入,也将在将其捕获为无效输入之后向用户提供建议。另外,我对Python还是很陌生,因此,对其余代码的任何建议将不胜感激。下面是我编写的代码:
from __future__ import division #Allows for division to return a float value
from colorama import init,Fore,Style,Back #Allows for formating color and stylizing text output to terminal screen
init(convert=True) #Allows Colorama to work on Windows 10 machine
import os
running=True
def printAnswer(sign,userInput1,userInput2,answer):
"Prints the formated answer to the screen"
print
print Fore.RED, Style.BRIGHT,userInput1, sign, userInput2, "=", answer, "\n" #Changes text to red
print Style.RESET_ALL #Changes text back to normal from Red
try:
input= raw_input("Press any key to continue")
except NameError:
pass
def printAnswerRemainder(sign,userInput1,userInput2,answerInt,remainder):
"Prints the formated division answer with remainder"
print Fore.Red, Style.BRIGHT, "\n", userInput2, sign, userInput1, "=", answerInt," Remainder ", remainder, "\n" #Changes text color to red
print Style.RESET_All #Resets text color back to normal
def newAdd(userInput1,userInput2):
"Performs the addition function"
sign="+"
answer=userInput1+userInput2
printAnswer(sign,userInput1,userInput2,answer)
def newSub(userInput1,userInput2):
"Performs the Subtraction function"
sign="-"
answer=userInput1-userInput2
printAnswer(sign,userInput1,userInput2,answer)
def newDivision(userInput1, userInput2):
"Performs divsion function giving a decimal answer and an answer with the remainder"
sign="/"
answer=userInput2/userInput1
answerInt=userInput2//userInput1
remainder=userInput2%userInput1
printAnswerRemainder(sign,userInput1,userInput2,answerInt,remainder)
printAnswer(sign, userInput2, userInput1, answer)
def newMult(userInput1,userInput2):
sign="X"
answer=userInput1*userInput2
printAnswer(sign,userInput1,userInput2,answer)
while running==True:
os.system('cls' if os.name == 'nt' else 'clear') #Clears the terminal of previous activity
userSelect=raw_input("Please enter the number of the type of operation you would like to complete:\n\t 1.Addition\n\t 2.Subtraction\n\t 3.Division\n\t 4.Multiplication\n\t 5.Exit\n\n-------> ")
if userSelect=="1":
addNum1=input("Enter the first number to add:\n")
addNum2=input("Enter the second nummebr to add:\n")
newAdd(addNum1,addNum2)
elif userSelect=="2":
subNum1=input("Enter the number to subtract from: \n")
subNum2=input("Enter the number you would like to subtract: \n")
newSub(subNum1,subNum2)
elif userSelect=="3":
divNum1=input("Enter the dividend: \n")
divNum2=input("Enter the divisor: \n")
newDivision(divNum2,divNum1)
elif userSelect=="4":
multNum1=input("Enter the number that is being multiplied: \n")
multNum2=input("Enter the number to be multiplied by: \n")
newMult(multNum1,multNum2)
elif userSelect=="5":
running=False
**else:
print "The command was invalid"
try:
input= raw_input("Press any key to continue")
except NameError:
pass**
答案 0 :(得分:1)
在此else子句中,您将覆盖内置函数input
:
try:
input= raw_input("Press any key to continue")
相反,这应该可以正常工作:
try:
anykey = raw_input("Press any key to continue")