0

I have written this program that takes a file called Sonnets and firstly, changes all the roman numerals in the sonnets to numbers, copies that to a new file, then asks the user for number input and shows them the sonnet corresponding to their number (if between 1 and 7).

For numbers outside 1 and 7, I would display appropriate error messages, giving specific directions on what they inputted and what they need to input again.

I wrote all of my functions separately, and everything runs when I put them together EXCEPT for the "except" part in function serve_poem(). It gave me the correct error message when I ran the function separately, however, now it just gives me the automatic error message instead of the specific message I encoded.

I posted my whole code below, because I figure that something in one of the other functions is messing with it(???) because it ran fine on its own.

def change_romans_to_numbers(s):
    if   s == "I.":     
        return("1.")
    elif s == "II.":    
       return("2.")
    elif s == "III.":   
        return("3.")
    elif s == "IV.":   
        return("4.")
    elif s == "V.":    
        return("5.")
    elif s == "VI.":    
        return("6.")
    elif s == "VII.":   
        return("7.")
    else:               
        return s

def serve_poem():
    sonnet=open(r"C:\Users\Emily\Documents\sonnets.txt", "r")
    x=int(input("Please enter a number 1-7:"))
    s=sonnet.readlines()
    s=list(s)
    try:
        if x==1:
            up=int(2+14*(x-1))
            lower=int(2+14*(x-1)+14)
            for i in range (up,lower):
                print(s[i])
        if 2<=x<=7:
            up=int((2+14*(1-1))+(19*(x-1)))
            lower=int((2+14*(1-1)+14)+(19*(x-1)))
            for i in range (up,lower):
                print(s[i])
        if x<0:
            print("You entered a negative number. Please enter a number between 1 and 7:")
            serve_poem()
        if x==0:
            print("You entered 0. Please enter a number between 1 and 7:")
            serve_poem()
        if x>7:
            print("You entered a number greater than 7. Please enter a number between 1 and 7:")
            serve_poem()
    except ValueError:
        print("Error: Value Error. You did not enter a number at all! Please re-enter a number:")
        serve_poem()

def writing_romans_to_numbers():        
    sonnet=open(r"C:\Users\Emily\Documents\sonnets.txt", "r")
    sonnet_fixed=open(r"C:\Users\Emily\Documents\sonnets-fixed.txt", "w")
    for line in sonnet:
        new=change_romans_to_numbers(line.strip())
        sonnet_fixed.write(new + '\n')

def main():
    writing_romans_to_numbers()
    serve_poem()

main()

Here is my error message (if user inputs q):

File "C:/Users/Emily/.spyder2-py3/temp.py", line 28, in serve_poem
x=int(input("Please enter a number 1-7:"))

ValueError: invalid literal for int() with base 10: 'q'
Megan
  • 231
  • 2
  • 5
  • 12
  • 2
    What do you mean by *the automatic error message*? Can you please include the **full** output of that message in your question? – Martijn Pieters Mar 15 '15 at 09:06
  • 1
    You probably should read all of [Asking the user for input until they give a valid response](http://stackoverflow.com/q/23294658), it'll teach you a lot about how to take end-user input, including why you shouldn't recursion to handle repetition. – Martijn Pieters Mar 15 '15 at 09:08
  • I have updated my question with my error message. – Megan Mar 15 '15 at 09:12
  • 1
    You should put `x=int(input("Please enter a number 1-7:"))` inside the `try` block, just before line `if x == 1`. Also, because the `serve_poem` function is recursive, the sonnets file is opened multiple times and not closed, which is bad. – Yegor Roganov Mar 15 '15 at 09:12
  • Thanks Martijn- that post answered my question. Apologize for the repeat – Megan Mar 15 '15 at 09:13

1 Answers1

0

Your problem is that you aren't wrapping this line in the try...except block:

    x=int(input("Please enter a number 1-7:"))

This line will raise an exception if given non-numeric input, but this exception will not handled by your try...except block. You would only need to put that one line in a try...except block, if that line passes it will be guaranteed to be numeric input, so your comparisons should work.

However, as Martjin said, that isn't the best approach.

TheBlackCat
  • 9,791
  • 3
  • 24
  • 31