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

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...
***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,...
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 having difficulty trying to make my code work. Create a subclass of Phone called SmartPhone....
I'm having difficulty trying to make my code work. Create a subclass of Phone called SmartPhone. Make this subclass have a no-arg constructor and a constructor that takes a name, email, and phone. Override the toString method to return the required output. Given Files: public class Demo1 { public static void test(Phone p) { System.out.println("Getter test:"); System.out.println(p.getName()); System.out.println(p.getNumber()); } public static void main(String[] args) { Phone test1 = new SmartPhone(); Phone test2 = new SmartPhone("Alice", "8059226966", "[email protected]"); System.out.println(test1); System.out.println(test2); System.out.println(test1);...
I'm working on a to-do list program in Python 2. I'm trying to delete an item...
I'm working on a to-do list program in Python 2. I'm trying to delete an item from the list and I'm not sure what I'm missing to do that. I haven't been able to get it to delete by string or by index number. Also, I'm trying to get the menu to run again after the user completes the add/delete/etc options. Do I need to put menu() menu_option = int(input("Welcome to your To-Do List. Please choose and option to continue:...
This is a question for my biochemistry class. I have copied and pasted it below. This...
This is a question for my biochemistry class. I have copied and pasted it below. This is all that was given to me. Diagram the flow of genetic material in a cell.
Using Python, I am trying to integrate 'iloc' into the line of code below? This line...
Using Python, I am trying to integrate 'iloc' into the line of code below? This line is replacing all the '1' values across my .csv file and is throwing off my data aggregation. How would I implement 'iloc' so that this line of code only changes the '1's integers in my 'RIC' column? patient_data_df= patient_data_df.replace(to_replace=[1], value ="Stroke") Thank you:)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT