Skip to content Skip to sidebar Skip to footer

Python – Have A Variable Be Both An Int And A Str

Here is my code: def retest2(): print 'Type in another chapter title! Or type \'Next\' to move on.' primenumbers2() def primenumbers1(): print '------------------------

Solution 1:

You need to use raw_input for python2, input in python2 is basically eval(raw_input()) and as you have no variable okay defined you get the error.

In [10]: prime = (str(input("\n")))

foo
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-10-15ff4b8b32ce> in <module>()
----> 1 prime = (str(input("\n")))

<string> in <module>()

NameError: name 'foo' is not defined    
In [11]: foo = 1 
In [12]: prime = (str(input("\n")))
foo
In [13]: prime = (raw_input("\n"))
next
In [14]: print prime
next

You should rarely if ever use input.

You should also use a while loop, something like the following:

 def primenumbers2():
    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)
    while True:
        prime = input("Type in another chapter title! Or type \"Next\" to move on.")
        if "next"  == prime.lower():
            print("--------------------------------------------------\nOnto the next thing.")
            break
        try:
            p = int(prime)
            if p in chapter:
                print("Chapter {}".format(chapter.index(p) + 1))
            else:
                 print("That is not one of my chapter numbers because {0}"
                  " is not a prime number. Try again.".format(prime))
        except ValueError:
            print("Invalid input")

Not totally sure what you want to do but using a while, checking if the user input is equal to next and if not trying to cast to int using a try/except is a better idea.

Making chapters a dict would also be a better idea, accessing the chapter number by key:

def primenumbers2():
    chaps = (
        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)

    chapter = dict(zip(chaps, range(1, len(chaps) + 1)))
    while True:
        prime = input("Type in another chapter title! Or type \"Next\" to move on.")
        if "next" == prime.lower():
            print("--------------------------------------------------\nOnto the next thing.")
            break
        try:
            p = int(prime)
            if p in chapter:
                print("Chapter {}".format(chapter[p]))
            else:
                print("That is not one of my chapter numbers because {}"
                      " is not a prime number. Try again.".format(prime))
        except ValueError:
            print("Invalid input")

range(1, len(chaps) + 1)) will mean each p in chapter will have a value corresponding to its index in the tuple.


Solution 2:

What you're trying to do is converting a value entered by the user to a string or an int. If you use input, which as Padraic says is equals to eval(raw_input()), you will eval the input as it was a python command: this will throw an error if you write any non existing name. To resolve this, use the raw_input function. It will return a str object, the input text.

Then, you want to find if this text is a number, or a string. One solution is using exceptions :

try:
    prime = int(prime)

    # Here the code assuming prime is a number, an `int`
except ValueError:
    # here the code assuming prime is a string, `str`, for example
    # 'Next', 'next' or 'okay'

Solution 3:

Try using isdigit to avoid the exception:

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 prime.isdigit() and int(prime) in chapter:
    print "Chapter ",chapter.index(int(prime)) + 1
    retest2()
else:
    print "That is not one of my chapter numbers because {0} is not a prime number. Try again.".format(prime)
    primenumbers2()

Post a Comment for "Python – Have A Variable Be Both An Int And A Str"