CodeSolved

Solved Programming Questions & Exercises

Manage the division errors

Practice Easy 2043/ Download 333 Views
Write a program that takes two from the user and divides them. Manage possible errors (divided by zero and invalid input).

7 Answers

try:
    num_1=int(input("enter first number>>>"))
    num_2=int(input("enter second number>>>"))
    result=num_1/num_2
except ZeroDivisionError:
    print("division to 0 isn't ok")
except ValueError:
    print("the dadta must be a integer")

Shina Download Python
try :
    num1 = float(input('Enter Number : '))
    num2 = float(input('Enter Number : '))
    print(num1 / num2)
except ZeroDivisionError:
    print('ERROR : Divisoin by zero')
except ValueError:
    print('ERROR : invalid input')

<!DOCTYPE html>
<html lang="fa" dir="rtl">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>تقسیم دو عدد</title>
  <link href="https://fonts.googleapis.com/css2?family=Vazirmatn&display=swap" rel="stylesheet">
  <style>
    * {
      box-sizing: border-box;
    }

    body {
      margin: 0;
      padding: 0;
      font-family: 'Vazirmatn', sans-serif;
      background: linear-gradient(135deg, rgb(44, 58, 54),rgb(12, 42, 58));
      min-height: 100vh;
      display: flex;
      align-items: center;
      justify-content: center;
    }

    form {
      background: rgba(255, 255, 255, 0.15);
      backdrop-filter: blur(10px);
      border-radius: 20px;
      padding: 30px 40px;
      width: 90%;
      max-width: 400px;
      box-shadow: 0 8px 32px rgba(0, 0, 0, 0.25);
      color: #e6dede;
      text-align: center;
    }

    label {
      display: block;
      margin: 15px 0 5px;
      font-size: 16px;
      font-weight: bold;
    }

    input[type="number"] {
      width: 100%;
      padding: 10px;
      border: none;
      border-radius: 10px;
      font-size: 16px;
      outline: none;
      background: rgba(255, 255, 255, 0.956);
    }

    input[type="submit"] {
      margin-top: 20px;
      width: 100%;
      padding: 12px;
      background: linear-gradient(to left, #11998e, #38ef7d);
      border: none;
      color: white;
      font-size: 18px;
      font-weight: bold;
      border-radius: 12px;
      cursor: pointer;
      transition: all 0.3s ease;
    }

    input[type="submit"]:hover {
      transform: scale(1.03);
      box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
    }

    #result {
      margin-top: 20px;
      font-size: 18px;
      font-weight: bold;
      color: rgb(235, 245, 255);
    }
  </style>
</head>
<body>

<form id="myForm">
  <label for="fn">عدد اولتان را وارد کنید :</label>
  <input type="number" id="fn">

  <label for="sn">عدد دومتان را وارد کنید:</label>
  <input type="number" id="sn">

  <input type="submit" value="محاسبه تقسیم">
  <div id="result"></div>
</form>

<script>
  const form = document.getElementById("myForm");

  form.addEventListener("submit", function (e) {
    e.preventDefault();

    const first_number = parseFloat(document.getElementById("fn").value);
    const second_number = parseFloat(document.getElementById("sn").value);
    const resultDiv = document.getElementById("result");
    let result;

    if (isNaN(first_number) || isNaN(second_number)) {
      result = "لطفاً هر دو عدد را وارد کنید.";
    } else if (second_number === 0) {
      result = "خطا! تقسیم بر صفر مجاز نیست.";
    } else {
      result = `نتیجه: ${first_number / second_number}`;
    }

    resultDiv.textContent = result;
  });
</script>

</body>
</html>

x = int(input('number 1: '))
z = int(input('number 2: '))

if z == 0:
    print('error: division by zero')
else:
    m = x // z
    print('نتیجه:', m)

User 4960 Download Python
num1 = int(input(Enter a number :))
num2 = int(input(Enter your number:))
answer = num1 / num2
if num2 = 0 :
  print ("Unacceptable")
else:
  print(answer
Amin1387 Download Python

This code has several syntax errors. The first is the first and second line -up .... The second is the conditional order that means the zero and the num 2 should actually be used ==, and the last error that does not actually do not indicate the error is that the conditional order only said if NUM2 was zero, and if we do not. Correct the code is: num1 = int (input ('Enter a number:')) num2 = int (input ('Enter your number:')) Answer = num1 / num2 if num2 == 0: Print ('unacceptable') ELSE: PRINT (Answer) User 5045


while True:
    try:                                                       
    y = int(input("number 1: "))
        x = int(input("number 2: "))
        op = input("operation ")
        if op == "/" and x != 0:
            print(y / x)
            break
        else:
            print("invaild syntax:pleas try again")
    except ValueError:
print("pleas enter number ")

User 4817 Download Python
def sum(a,b):
    try:
        return a / b
    except ZeroDivisionError as zde:
        print(f"ZeroDivisionError has detected! Error:{zde}")

ask1 = int(input("a : "))
ask2 = int(input("b : "))
sum(ask1, ask2)
Macsepehri Download Python

(PRINT (SUM (ASK1, ASK2) User 5045


Submit answer

Submitting answers is currently unavailable.

×
×
Close