Question

Help, I just cannot figure out how to make this code print "x cannot be divided...

Help, I just cannot figure out how to make this code print "x cannot be divided by zero" after i set the y intercept value to 0 and i want the code to print "x cannot be divided by zero" everytime i put a zero for y value.

Here is my code below: (PYTHON)

x_intercept = float(input("Enter x: "))
y_intercept = float(input("Enter y: "))
operation_msg = input("Enter operation (plus, power, divide): ")
add_formula = x_intercept + y_intercept
add_final = float(add_formula)
power_formula =(x_intercept ** y_intercept)
power_final = float(power_formula)
divide_formula = x_intercept / y_intercept
divide_final = float(divide_formula)

divison_form = y_intercept
if operation_msg == "divide":
print("x divided by y is : ", divide_form)
if divison_form == 0:
print("does not work")

elif operation_msg == "plus":
print("x plus y:", add_final)

elif operation_msg == "power":
print("x to the power of y is", power_final)

else:
print ("I don't recognize that operation.")

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:

You should put the variables inside if else, otherwise, everytime all the operations will be performed and divide by 0 will give error.

Do it like this:


x_intercept = float(input("Enter x: "))
y_intercept = float(input("Enter y: "))
operation_msg = input("Enter operation (plus, power, divide): ")

divison_form = y_intercept
if operation_msg == "divide":
if divison_form == 0:
print("x cannot be divided by zero")
else:
divide_formula = x_intercept / y_intercept
divide_final = float(divide_formula)
print("x divided by y is : ", divide_final)
elif operation_msg == "plus":
add_formula = x_intercept + y_intercept
add_final = float(add_formula)
print("x plus y:", add_final)

elif operation_msg == "power":
power_formula =(x_intercept ** y_intercept)
power_final = float(power_formula)
print("x to the power of y is", power_final)
else:
print ("I don't recognize that operation.")

Output:

PLEASE UPVOTE IF YOU FOUND THIS HELPFUL!

Add a comment
Know the answer?
Add Answer to:
Help, I just cannot figure out how to make this code print "x cannot be divided...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using Python The variables x and y refer to numbers. Write a code segment that prompts...

    Using Python The variables x and y refer to numbers. Write a code segment that prompts the user for an arithmetic operator and prints the value obtained by applying that operator to x and y. Using the following information run an example in Python Idle op = input("Enter an arithmetic operator: ") if op == "+":     print(x + y) elif op == "-":     print(x – y) elif op == "*":     print(x * y) elif op == "/":...

  • Hello, In Python Enhance the prior assignment by doing the following 1) Create a class that...

    Hello, In Python Enhance the prior assignment by doing the following 1) Create a class that contain all prior functions MyLib # This function adds two numbers def addition(a, b): return a + b # This function subtracts two numbers def subtraction(a, b): return a - b # This function multiplies two numbers def multiplication(a, b): return a * b # This function divides two numbers # The ZeroDivisionError exception is raised when division or modulo by zero takes place...

  • I need help finding 7 errors in the following python code. Please provide indentation in your...

    I need help finding 7 errors in the following python code. Please provide indentation in your response. totalRounds = 0 humanWins = 0 humanLosses = 0 humanTies = 0 computerWins = 0 computerLosses = 0 computerTies = 0 uc = 0 cc = 0 rndNbr = 0 print('Welcome to the Rock-Paper-Scissors Game!!') print('Have fun and good luck.\n') totalRounds = int(input('Please enter the number of rounds you must need to win the match:')) while humanWins != totalRounds or computerWins != totalRounds:...

  • Please make this Python code execute properly. User needs to create a password with at least...

    Please make this Python code execute properly. User needs to create a password with at least one digit, one uppercase, one lowercase, one symbol (see code), and the password has to be atleast 8 characters long. try1me is the example I used, but I couldn't get the program to execute properly. PLEASE INDENT PROPERLY. def policy(password): digit = 0 upper = 0 lower = 0 symbol = 0 length = 0 for i in range(0, len(password)): if password[i].isdigit(): # checks...

  • AND logic gate simulation in Python: Please fix the code below so that it efficiently performs...

    AND logic gate simulation in Python: Please fix the code below so that it efficiently performs the operation of the AND logic gate in Python. (Leave comments in the edited code about what you changed) #if both values are 1, return true, other wise return false print ("Logic Gate Simulation: AND gate") def AND(x, y):     if x == True and y == True:         return True        else:         return False    def main():     x = bool(input("Please...

  • Hey i have a project due, and for the life of me i cannot figure out...

    Hey i have a project due, and for the life of me i cannot figure out how to do this with the restrictions that are put in place, shiftwise operators such as << and >> <<<< >>>> cannot be used This is the question that follows: Shifter Implement a 32-bit shifter. Input: x (32 bits), c (5 bits), and op (2 bits). Output: y. This shifter should shift the input number x by c bits according to the shift operations...

  • THIS IS THE CODE I AM TRYING TO RUN code # def main():       #Reading a as...

    THIS IS THE CODE I AM TRYING TO RUN code # def main():       #Reading a as an integer       a = int(input("Enter value for a: "))       #reading b as an interger       b = int(input("Enter value for b: "))       #reading n as an integer       n = int(input("Enter value for n: "))       # displaying if a and b are congruent modulo n, this is True if a and b       #both returns same remainder when divided by n       if a % n == b...

  • [Using Python] I need my code given below to loop the user input, so that you...

    [Using Python] I need my code given below to loop the user input, so that you are asked to input without it stopping after 4 inputs. Code: #A program that converts a hexadecimal number to its decimal value #Define function for hexadecimal to decimal def hexToDec(hexi): result = 0 #For loop to test input for correct values for char in hexi: if 'A' <= char <= 'F' or 'a' <= char <= 'f': if 'a' <= char <= 'f': result...

  • Python 3 Can anyone make my Python code more readable, by breaking up the code into...

    Python 3 Can anyone make my Python code more readable, by breaking up the code into smaller chunks using functions. If a piece of code tries to do several things, it should be broken up into several different functions. print("chatbot: Hello, I am chatbot. What is your name?") name = input("") print("chatbot: Nice to meet you "+name+". Tell me now, what is your age?") age = int(input("")) if age >= 16 and age <= 25: print("We are the same age!...

  • this code is not working for me.. if enter 100 it is not showing the grades...

    this code is not working for me.. if enter 100 it is not showing the grades insted asking for score again.. #Python program that prompts user to enter the valuesof grddes . Then calculate the total scores , #then find average of total score. Then find the letter grade of the average score. Display the #results on python console. #grades.py def main(): #declare a list to store grade values grades=[] repeat=True #Set variables to zero total=0 counter=0 average=0 gradeLetter='' #Repeat...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT