Oct-30-2017, 08:52 PM
(This post was last modified: Oct-30-2017, 09:19 PM by sparkz_alot.)
Hi!
Doing homework for programming class, having problems with try-except. So the program works fine, it opens the txt file and searches for the correct city, however, the except line does not work. So if you enter a name of city that is not in the txt file of cities it should go to the except line and print "wrong" but it doesn't. Could anybody help pls? Thanks! **wall**
city = raw_input ("Enter the name of the city: ")
ffile = open ("cities.txt")
try:
for line in ffile:
if line.startswith (city):
d1 = line.split (".")
d2 = d1 [1]
d3 = int(d2)
print d3
except:
print "wrong"
Doing homework for programming class, having problems with try-except. So the program works fine, it opens the txt file and searches for the correct city, however, the except line does not work. So if you enter a name of city that is not in the txt file of cities it should go to the except line and print "wrong" but it doesn't. Could anybody help pls? Thanks! **wall**
city = raw_input ("Enter the name of the city: ")
ffile = open ("cities.txt")
try:
for line in ffile:
if line.startswith (city):
d1 = line.split (".")
d2 = d1 [1]
d3 = int(d2)
print d3
except:
print "wrong"