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'