In: Computer Science
Invalid entry code in python
my code is pasted below. The last elif statement, I'm trying to get the program to print "invalid entry" if the entry for user_input is invalid. The first user prompt should only allow for numbers 1-10 and "exit" and "quit"
import math
user_prompt = """Enter number of operation that you want to execute
<type exit or quit to end program>:
1 sin(x)
2 cos(x)
3 tan(x)
4 asin(x)
5 acos(x)
6 atan(x)
7 ln(x)
8 sqrt(x)
9 factorial(x)
10 history
Select-> """ #prompting user
results = []
valid_response = ['1, 2, 3, 4, 5, 6, 7, 8, 9, 10, quit, exit']
while True:
user_input = input(user_prompt)
print(f'User input is {user_input}')
if user_input.lower() == 'quit' or user_input == 'exit':
print()
break #terimates the program due to quit or exit response
elif user_input == '10': # displaying the history
for i in results:
print(i)
continue
user_num_text = input("\nEnter the argument for operation ->
")
user_number = float(mcs_num_text)
if user_input == '1':
result = math.sin(math.radians(user_number))
temp = f'sin({user_number}) = {result}' #sin
elif user_input == '2':
result = math.cos(math.radians(user_number))
temp = f'cos({user_number}) = {result}' #cos
elif user_input == '3':
result = math.tan(math.radians(user_number))
temp = f'tan({user_number}) = {result}' #tan
elif user_input == '4':
result = math.asin(math.radians(user_number))
temp = f'asin({user_number}) = {result}' #asin
elif user_input == '5':
result = math.acos(math.radians(user_number))
temp = f'acos({user_number}) = {result}' #acos
elif user_input == '6':
result = math.atan(math.radians(user_number))
temp = f'atan({user_number}) = {result}' #atan
elif user_input == '7':
result = math.log(float(user_number))
temp = f'ln({user_number}) = {result}' #ln
elif user_input == '8':
result = math.sqrt(float(user_number))
temp = f'sqrt({user_number}) = {result}' #sqrt
elif user_input == '9':
result = math.factorial(int(user_number))
temp = f'factorial({user_number}) = {result}' #factorial
elif user_input is not valid_response:
temp = 'Invalid entry.'
print(temp)
results.append(temp)
The problem is with user_input us not valid_response statement. is not should be replaced with <not in>. Here is the modified code:
import math
user_prompt = """Enter number of operation that you want to execute <type exit or quit to end program>:
1 sin(x)
2 cos(x)
3 tan(x)
4 asin(x)
5 acos(x)
6 atan(x)
7 ln(x)
8 sqrt(x)
9 factorial(x)
10 history
Select-> """
results = []
valid_response = ['1, 2, 3, 4, 5, 6, 7, 8, 9, 10, quit, exit']
while True:
user_input = input(user_prompt)
print(f'User input is {user_input}')
if user_input.lower() == 'quit' or user_input == 'exit':
print()
break
elif user_input == '10': # displaying the history
for i in results:
print(i)
continue
if user_input in ['1','2','3','4','5','6','7','8','9']:
user_num_text = input("\nEnter the argument for operation -> ")
user_number = float(user_num_text)
if user_input == '1':
result = math.sin(math.radians(user_number))
temp = f'sin({user_number}) = {result}' #sin
elif user_input == '2':
result = math.cos(math.radians(user_number))
temp = f'cos({user_number}) = {result}' #cos
elif user_input == '3':
result = math.tan(math.radians(user_number))
temp = f'tan({user_number}) = {result}' #tan
elif user_input == '4':
result = math.asin(math.radians(user_number))
temp = f'asin({user_number}) = {result}' #asin
elif user_input == '5':
result = math.acos(math.radians(user_number))
temp = f'acos({user_number}) = {result}' #acos
elif user_input == '6':
result = math.atan(math.radians(user_number))
temp = f'atan({user_number}) = {result}' #atan
elif user_input == '7':
result = math.log(float(user_number))
temp = f'ln({user_number}) = {result}' #ln
elif user_input == '8':
result = math.sqrt(float(user_number))
temp = f'sqrt({user_number}) = {result}' #sqrt
elif user_input == '9':
result = math.factorial(int(user_number))
temp = f'factorial({user_number}) = {result}' #factorial
elif user_input is not valid_response:
temp = 'Invalid entry.'
print(temp)
results.append(temp)