【发布时间】:2014-06-16 21:56:40
【问题描述】:
我试图解决一些已经存在的关于我的错误的问题,但没有一个能解决问题。 这是我尝试运行的代码:
from random import *
location1 = randint(0,7)
location2 = location1 + 1
location3 = location2 + 1
guess = None
hits = 0
guesses = 0
isSunk = False
while (isSunk == False):
guess = raw_input("Ready, aim, fire! (enter a number from 0-6): ")
if (guess < 0 | guess > 6):
print "Please enter a valid cell number!"
else:
guesses = guesses + 1;
if (guess == location1 | guess == location2 | guess == location3):
print "HIT!"
hits = hits + 1
if (hits == 3):
isSunk = True
print "You sank my battleship!"
else:
print "MISS!"
stats = "You took " + guesses + " guesses to sink the battleship, " + "which means your shooting accuracy was " + (3/guesses)
print stats
我得到的错误是:
Traceback (most recent call last):
File "battleship.py", line 13, in <module>
if (guess < 0 | guess > 6):
TypeError: unsupported operand type(s) for |: 'int' and 'str'
我该如何解决这个问题?
【问题讨论】:
-
raw_input()返回一个str并且您正在与int进行比较。虽然这不会导致错误,但这不是您想要的。
标签: python python-2.7 typeerror