2016-01-07 2 views
0

Я нашел упражнение онлайн, чтобы сделать определенную программу, вроде как потерял письмо и забыл пройти тест слишком долго. Теперь программа выплевывает ошибку - и это очень странно.Синтаксис Ошибка в простом коде Python

Я не знал, сколько из них можно опубликовать, но я все это на всякий случай выкладываю.

import math 
print "Hey. Do you want to activate the hypothenuse program, or the diagonals one?" 
print "[1] Hypothenuse" 
print "[2] Diagonals" 
program_chosen = raw_input() 

if program_chosen == "1": 

    print """ 
     Hello. This is a simple math program to determine the length of the hypothenuse of 
     a right triangle given the other two sides. The results will be rounded to three decimal 
     places, to avoid confusion. 
     """ 
    # Once the other problems have been dealt with, implement a "choose digits rounded" option. 
    side1 = int(raw_input("Please enter the length of any of the non-hypothenuse sides of the triangle. ")) 
    side2 = int(raw_input("Now, enter the length of the other side. ")) 
    pythagoras = math.sqrt((side1**2)+(side2**2)) 
    # Need to define another variable to choose the number of rounded-to digits. 
    print "The length of the hypothenuse is , approximately, " + str((round(pythagoras, 3))) + "." 

elif program_chosen == "2": 
    print """ 
     Very well. The following program is one which, given an n-sided polygon, finds the number 
     of diagonals formed by its vertexes. 
     """ 
    n_of_sides = int(raw_input("To start, please enter the number of sides of the polygon. ")) 
    if n_of_sides < 3 
     print "That isn't a polygon, silly! Please try again." 
     # Need to find a way to repeat the prompt until a valid number of sides is inputted. 
     # Probably a While loop, but haven't learned how to use one effectively yet. 
     # Apparently a goto is not good programming form. :(
     exit() 
    else 
     n_of_diagonals = (n_of_sides*(n_of_sides-3))/2 
     print " A %d sided polygon has %d diagonals." % (n_of_sides, n_of_diagonals) 

else: 
    print "That is not a valid option. Please try again." 
    # Here, too, need to implement a "try again" option. 
    exit() 

Дело в том, при его запуске, PowerShell сказал мне строки 7 if выписки брейков (SyntaxError: недопустимый синтаксис), потому что я с помощью одного знака равенства (который я нашел переменный сеттер, а не сравнение), но даже после смены его на двойную равную единице ошибка продолжала появляться. Поэтому я попробовал еще раз, чтобы быть уверенным, что на тестировщике кода «после часа» появляется ошибка «ParseError: bad input on line 27».

Так что я действительно смущен. Пожалуйста помоги?

+0

Вы забыли ':' в конце вашего состояния. –

+2

Если вы запускаете/вставляете это в PowerShell (оболочку), он будет анализироваться как PowerShell (язык), а не Python –

+0

@ Vincent Savard: Welp, я идиот. Вы правы, программа работает отлично. Кроме того, SyntaxError в строке 7 состояла в том, что я запускал другую, более старую версию программы, где я еще не изменил ее на два одинаковых знака. Большое спасибо! – matacusa

ответ

2

В частности, вы пропустили два двоеточия:

if n_of_sides < 3: ####### 
     print "That isn't a polygon, silly! Please try again." 
     # Need to find a way to repeat the prompt until a valid number of sides is inputted. 
     # Probably a While loop, but haven't learned how to use one effectively yet. 
     # Apparently a goto is not good programming form. :(
     exit() 
    else: ##### 
+0

Да, сделано и сделано. Благодаря! – matacusa

Смежные вопросы