Having trouble making elseif statements run in Python

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

I m very new to Python and am trying to teach myself a few things using various online resources. In the WIkipedia article on algorithms, there is a sample BASIC program; I ve decided to try and write the same program using Python, but I am running into problems with the syntax of my if/else statements. I m pretty sure it s a basic formatting problem, but I don t have enough experience with coding to understand what I m doing wrong. The following chunk of code:

# Euclid s algorithm for greatest common divisor

print "Euclid s algorithm for greatest common divisor"

print "Type two integers greater than 0"
("
")
("a")

# Gather input from user in the form of a string. 

("
")
a = raw_input("Integer 1? ")
("
")
b = raw_input("Integer 2? ")
("
")

# Calculate equalities.

if b = 0:
    print a

elif a > b:
a = a - b
print a

b = b - a

if b = 0:
print a

returns the error:

  File "euclid.py", line 35
    if b = 0:
         ^
SyntaxError: invalid syntax

I realize the module as a whole is incomplete, but I would like to try to figure out what I m doing wrong in this part before I move on to the next part.

Answers

Two issues:

if b = 0: # this is assignment; you want == which is comparison
    print a

elif a > b:
a = a - b # this needs to be indented just like the print under the if clause

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/17795663/having-trouble-making-else-if-statements-run-in-python

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils