Question

In: Computer Science

Invalid entry code in python my code is pasted below. The last elif statement, I'm trying...

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)

Solutions

Expert Solution

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)

Related Solutions

In Python I have a code: here's my problem, and below it is my code. Below...
In Python I have a code: here's my problem, and below it is my code. Below that is the error I received. Please assist. Complete the swapCaps() function to change all lowercase letters in string to uppercase letters and all uppercase letters to lowercase letters. Anything else remains the same. Examples: swapCaps( 'Hope you are all enjoying October' ) returns 'hOPE YOU ARE ALL ENJOYING oCTOBER' swapCaps( 'i hope my caps lock does not get stuck on' ) returns 'I...
Hello! I'm trying to work on a python lab in my class, and we basically have...
Hello! I'm trying to work on a python lab in my class, and we basically have to make this method play_interactive Now for the fun bit. The function play_interactive will take just one argument --- the length of patterns to use as keys in the dictionary --- and will start an interactive game session, reading either '1' or '2' from the player as guesses, using the functions you wrote above and producing output as shown in the sample game session...
***The code is provided below*** When trying to compile the code below, I'm receiving three errors....
***The code is provided below*** When trying to compile the code below, I'm receiving three errors. Can I get some assistance on correcting the issues? I removed the code because I thought I corrected my problem. I used #define to get rid of the CRT errors, and included an int at main(). The code compiles but still does not run properly. When entering the insertion prompt for the call details, after entering the phone number, the program just continuously runs,...
This is my code for python. I am trying to do the fourth command in the...
This is my code for python. I am trying to do the fourth command in the menu which is to add an employee to directory with a new phone number. but I keep getting error saying , "TypeError: unsupported operand type(s) for +: 'dict' and 'dict". Below is my code. What am I doing wrong? from Lab_6.Employee import * def file_to_directory(File): myDirectory={}       with open(File,'r') as f: data=f.read().split('\n')    x=(len(data)) myDirectory = {} for line in range(0,199):      ...
Trying to score a hand of blackjack in this python code but my loop is consistently...
Trying to score a hand of blackjack in this python code but my loop is consistently outputting (13,1) which makes me think that something is wrong with my loop. Could someone help me with this code?: import random cards = [random.randint(1,13) for i in range(0,2)] #initialize hand with two random cards def get_card(): #this function will randomly return a card with the value between 1 and 13 return random.randint(1,13) def score(cards): stand_on_value = 0 soft_ace = 0 for i in...
this is my code in python I am trying to open a file and then print...
this is my code in python I am trying to open a file and then print the contents on my console but when i run it nothing prints in the console def file_to_dictionary(rosterFile): myDictionary={}    with open(rosterFile,'r') as f: for line in f: myDictionary.append(line.strip()) print(myDictionary)             return myDictionary    file_to_dictionary((f"../data/Roster.txt"))      
XML and XSL I'm trying to style my XML code but it's not implementing the XSL...
XML and XSL I'm trying to style my XML code but it's not implementing the XSL file when I run it even though the file is referenced. Any help? XML code: <?xml version="1.0"?> <?xml-stylesheet href="textbooks.xsl" type="text/xsl" ?> <textbooks> <textbook> <title> Introduction to Design and Analysis of Algorithms</title> <authors> <author> <firstName>Anany</firstName> <lastName>Levitin</lastName> </author> </authors> <publisher> <name>Ed, Pearson</name> <website>https://www.pearson.com</website> </publisher> <Year-of-Publication>2011</Year-of-Publication> <ISBN>978-0132316811</ISBN> <book-specific-website></book-specific-website> <edition>3rd edition</edition> <cover-type>Paperback</cover-type> </textbook> <textbook> <title>Software Engineering: A Practitioner’s Approach</title> <authors> <author> <firstName>Roger</firstName> <lastName>Pressman</lastName> </author> </authors> <publisher> <name>Ed, McGraw-Hill</name>...
Working with Python. I am trying to make my code go through each subject in my...
Working with Python. I am trying to make my code go through each subject in my sample size and request something about it. For example, I have my code request from the user to input a sample size N. If I said sample size was 5 for example, I want the code to ask the user the following: "Enter age of subject 1" "Enter age of subject 2" "Enter age of subject 3" "Enter age of subject 4" "Enter age...
Hi, I'm trying to rewrite the code below (code #1) by changing delay() to millis(). void...
Hi, I'm trying to rewrite the code below (code #1) by changing delay() to millis(). void loop() { // Print the value inside of myBPM. Serial.begin(9600); int myBPM = pulseSensor.getBeatsPerMinute(); // Calls function on our pulseSensor object that returns BPM as an "int". // "myBPM" hold this BPM value now. if (pulseSensor.sawStartOfBeat()) { // Constantly test to see if "a beat happened". Serial.println("♥ A HeartBeat Happened ! "); // If test is "true", print a message "a heartbeat happened". Serial.print("BPM:...
I'm trying to get my code to loop back to let the user input multiple patients....
I'm trying to get my code to loop back to let the user input multiple patients. import java.util.Scanner; public class XYZ {    public static void main(String[] args) {           String response;        do{            //import scanner for input            Scanner input = new Scanner(System.in);               //prompt user input for last name            System.out.print("Enter last name ");            //output for last name            String...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT