Diskussion:Mittpunktsformeln i Python

Från Wikiskola
Hoppa till navigering Hoppa till sök

Bråkutskrift och hantering av felaktig input

from fractions import Fraction

print('\nCalculate the midpoint of a line :')

while True:
    try:
        x1 = float(input('The value of x (the first endpoint) '))
        break
    except ValueError:
        print("Enter a valid number")
        continue
while True:
    try:
        y1 = float(input('The value of y (the first endpoint) '))
        break
    except ValueError:
        print("Enter a valid number")
        continue
while True:
    try:
        x2 = float(input('The value of x (the second endpoint) '))
        break
    except ValueError:
        print("Enter a valid number")
        continue
while True:
    try:
        y2 = float(input('The value of y (the second endpoint) '))
        break
    except ValueError:
        print("Enter a valid number")
        continue
        

x_m_point = (x1 + x2)/2
y_m_point = (y1 + y2)/2
print();
print("The midpoint of line is :")
print( "The midpoint's x value is: ",Fraction(x_m_point))
print( "The midpoint's y value is: ",Fraction(y_m_point))
print();