Python - 变量既可以是int也可以是strPython – Have a variable be both an int and a str

``````def retest2():
print "Type in another chapter title! Or type \"Next\" to move on."
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."
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)

``````

``````File "prime.py", line x, in primenumbers2
prime = (str(input("\n")))
File "<string>", line 1, in <module>
NameError: name 'okay' is not defined
``````

``````That is not one of my chapter numbers because 4 is not a prime number. Try again.
``````

3 个回复3

===============>>#1 票数：2 已采纳

``````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
``````

`````` 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")
``````

``````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))`意味着章节中的每个`p`将具有与其元组中的索引相对应的值。

===============>>#2 票数：1

``````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'
``````

===============>>#3 票数：0

``````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)
``````

3回复

1回复

1回复

1回复

2回复

4回复

2回复

2回复

1回复

2回复