Question
Python program won't accept my user input and proceed through the program even if the condition is true. The program is asking for the user
Python program won't accept my user input and proceed through the program even if the condition is true. The program is asking for the user to select an arithmetic operation to select from a list. If the user inputs a number from the list, the program should proceed to ask them for information to perform the selected calculation. The issue I'm seeing is that even if the user inputs "1", the program will output the else statement of "Invalid entry. Please try again." Below is the code from my imported file and the main program:
Mylib.py:
# Function to add two user input numbers def add(fn, sn): answer = fn + sn print (fn, "+", sn, "is", answer) # Function to subtract two user input numbers def sub(fn, sn): answer = fn - sn print (fn, "-", sn, "is", answer) # Function to multiply two user input numbers def mult(fn, sn): answer = fn * sn print (fn, "*", sn, "is", answer) # Function to divide two user input numbers #Exception 1: If a user tries to divide by 0, they will get a "ZeroDivisionError: division by zero" exception. This has been handled below. def div(fn, sn): try: answer = fn / sn print (fn, "/", sn, "is", answer) except: print ("Divison of your two numbers could not be completed.", fn, "is not divisible by", sn,".") # This function will determine if the user-input numbers are within the user-defined upper and lower limits and then complete arithemtic operators def isInRange(lr,hr,fn,sn): if lr <= fn <= hr and lr <= sn <= hr: if(choice == 1): print(int(fn)+ " + " +int(sn) +" = "+ int(add(fn,sn))) if(choice == 2): print(int(fn)+" - "+ int(sn) +" = "+ int(sub(fn,sn))) if(choice == 3): print(int(fn)+ " * "+int(sn) +" = "+ int(mult(fn,sn))) if(choice == 4): try: print(int(fn)+ " / " +int(sn)+ " = "+ int(div(fn,sn))) except: print("Divison of your two numbers could not be completed.", fn, "is not divisible by", sn,".") if(choice == 5): res = allInOne(fn,sn) print(int(fn)+ " + " +int(sn) +" = "+ int(res["add"])) print(int(fn)+ " - " +int(sn) +" = "+ int(res["sub"])) print(int(fn)+ " * " +int(sn) +" = "+ int(res["mult"])) try: print(int(fn)+ " / " +int(sn) +" = "+ int(res["div"])) except: print("Divison of your two numbers could not be completed.", fn, "is not divisible by", sn,".") else: print ("The numbers you provided are not within your lower and higher limit.")
def scalc(p1): #this function takes string 'p1' as parameter fn,sn,operator=p1.split(",") #split the string on ',' fn=int(fn) #convert the first and second number into interger from string sn=int(sn) #call functions according to the operator in the string if operator=="+": add(fn,sn) elif operator=='-': sub(fn,sn) elif operator=='*': mult(fn,sn) elif operator=='/': div(fn,sn) else: print("Invalid Entry")
def allInOne(fn,sn): #If selected by user, this function will perform all operations. add = fn + sn sub = fn - sn mult = fn * sn try: div = fn / sn except: print ("Divison of your two numbers could not be completed.", fn, "is not divisible by", sn,".")
return {"add" : add, "sub" : sub, "mult" : mult, "div" : div}
def calculator(): print("Select operation, or type Q to quit the program.") #print("1.Add") print("1.Add 2.Subtract 3.Multiply 4.Divide 5.AllInOne")
calculator.py:
import Mylib
print("Welcome! This program will do simple math!")
Mylib.calculator() #Below in bold is where I'm having trouble.
choice = 0 while choice != "q" and choice != "Q": try: choice = input("Enter your choice: ") if choice != "q" and choice != "Q": if choice == 1 or choice == 2 or choice == 3 or choice == 4 or choice == 5: try: userInput = input("Please enter your lower range to continue:") lr = int(userInput) hr = int(input("Please enter your higher range:")) fn = int(input("Please enter your first number:")) sn = int(input("Please enter your second number:"))
Mylib.isInRange(lr,hr,fn,sn)
userInput = input("Please enter your lower range to continue, or 'Q' to quit.")#prompts user for input to complete input loop if userInput == "q" or userInput == "Q": continue except: print ("Invalid entry. Please try entering a whole number.") else: print ("Invalid entry. Please try again.") except: print ("Invalid entry. Please try again.")
print ("Thank you for using my calculator!")
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started