Here is my code:
def retest2():
print "Type in another chapter title! Or type \"Next\" to move on."
primenumbers2()
def primenumbers1():
print "--------------------------------------------------\nChapters in books are usually given the cardinal numbers 1, 2, 3, 4, 5, 6 and so on.\nBut I have decided to give my chapters prime numbers 2, 3, 5, 7, 11, 13 and so on because I like prime numbers.\n\nType in the chapter title of my book (a prime number) and I will tell you what cardinal number the chapter is."
def primenumbers2():
prime = (str(input("\n")))
chapter = (2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233)
if "Next" in prime or "next" in prime:
print "--------------------------------------------------\nOnto the next thing."
elif int(prime) in chapter:
print "Chapter ",chapter.index(int(prime)) + 1
retest2()
elif prime.isalpha:
print "That is not one of my chapter numbers because {0} is not a prime number. Try again.".format(prime)
primenumbers2()
primenumbers1()
primenumbers2()
So what I'm trying to do is having the user input a prime number and the output is the cardinal number correlating to that prime number. However, I want the user to have the option of going onto the next function by typing in "Next"
or "next"
. Therefore, my variable input prime
needs to accomodate for both string and integer input. So I set it as (str(input("\n")))
and then I converted it to int(prime)
when I needed to.
Everything works well except when the string input is anything but "Next"
or "next"
. For example, if I input "okay", I get the error message:
File "prime.py", line x, in primenumbers2
prime = (str(input("\n")))
File "<string>", line 1, in <module>
NameError: name 'okay' is not defined
But if I input "4", which is NOT a prime number, the program works and I get:
That is not one of my chapter numbers because 4 is not a prime number. Try again.
And the program loops back to primenumbers2()
to restart that function.
Please help me make this work!