我试着复习一些已经存在的关于我的错误的问题,但没有一个能做到。 这是我尝试运行的代码:
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
我得到的错误是:
^{pr2}$我怎么解决这个问题?在
您使用的是二进制或运算符。只需将“|”替换为“或”就可以了。在
在Python中,
|
是二进制或。您应该使用or
运算符,如下所示这条线也必须改变
^{pr2}$到
引用Binary bit-wise operator documentation
^{bq}$但是,通常这个声明是这样写的
另外,
raw_input
返回一个字符串。所以您需要显式地将其转换为int
,如下所示注意,在Python中不需要
;
来标记语句的结尾。在相关问题 更多 >
编程相关推荐