0

I'm a beginner with python. I've created a little program for my navigation homework which when I input the data solves it for me but it's not working. It asks the initial question to determine which type of problem to solve and then, when it's supposed to solve it, it crashes. Why is that?

import math

print("Insert all values in degree decimal form.")
prob = input("1 or 2")
if prob == 1:
    fia = input("Insert value of phi in A: ")
    lama = input("Insert value of lambda in A: ")
    route = input("Insert value of true route: ")
    vel = input("Insert speed: ")
    Tma = input("Insert Established Time of Departure: ")
    Tmb = input("Insert Established Time of Arrival: ")
    deltaT = Tmb - Tma
    print(deltaT)
    m = vel * deltaT
    print(m)
    deltafi = (m / 60) * math.cos(route)
    print(deltafi)
    fib = fia + deltafi
    if fib > 180:
        fib1 = -(360 - fib)
        fib = fib1
        print(fib)
    else:
        print(fib)
    fim = (fia + fib) / 2
    print(fim)
    deltalam = (m * math.sin(route) / (60 * math.cos(fim)))
    lamb = lama + deltalam
    if lamb > 180:
        lamb1 = -(360 - lamb)
        lamb = lamb1
        print(lamb)
    else:
        print(lamb)
    print("Coordinates of B: ")
    print(fib, lamb, Tmb)
elif prob == 2:
    fia = input("Insert value of phi in A: ")
    lama = input("Insert value of lambda in A: ")
    fib = input("Insert value of phi in B: ")
    lamb = input("Insert value of lambda B: ")
    deltafi = fib - fia
    deltalam = lamb - lama
    fim = (fia + fib) / 2
    tanroute = (deltalam / deltafi) * math.cos(fim)
    route = math.atan(tanroute)
    m = (60 * abs(deltafi)) / math.cos(route)
  • This sentence caught my interest: "when I input the data solves it for me but it's not working.". I take it as if you use either 1 or 2 as parameter it works, but when the teacher uses for example 3 it crashes? If so it is due to you not "covering" that possibility. If someone enters a value besides 1 or 2 it crashes. You need an else statement to cover those – Cenderze Jan 28 '21 at 11:44
  • 1
    The biggest problem that I see is that you need to convert the inputs to valid data types (int or float). Because the types of your inputs are strings – puchal Jan 28 '21 at 11:50
  • @Cenderze why would it crash if someone inputs 3? It simply doesn't catch any of the conditions... – Tomerikoo Jan 28 '21 at 11:51
  • @Tomerikoo Oh yes, that makes sense :) – Cenderze Jan 28 '21 at 12:01

3 Answers3

0

Input in python receives the value as string, and you are comparing it to integer value in your if condition

update your input statement to the following line to receive integer input:

prob = eval(input("1 or 2"))
0

Input method interpret your data as string, not as integer.

In your if condition you've to put

if int(prob) == 1:
Tajinder Singh
  • 1,361
  • 2
  • 14
  • 26
0

You have to convert the prob input (and all the other inputs) into int as it is considered a string by default: prob = int(input("...")).

You can also verify this by simply printing type(prob).