答案 0 :(得分:0)
这不是我会做的方式,但是考虑到您是一个初学者,我认为这是您在看书时可以理解的东西:
from random import randrange
def next_shot(shot, touch):
if shot == touch:
print("HIT!")
elif shot == (touch + 1):
print("HIT (+1)")
return True
elif shot == (touch - 1):
print("HIT (- 1)")
return True
elif shot == (touch + 10):
print("HIT (+ 10)")
return True
# You can add more else-if statements to declare other conditions
elif shot == (touch - 10):
print("HIT (- 10)")
return True
# If nothing hits, we return False
return False
# Start of your script
game_status = 'running'
while game_status == 'running':
shot = randrange(0, 99)
touch = randrange(0, 99)
print(f"Current shot is: {shot}\nCurrent touch is: {touch}")
if next_shot(shot, touch):
game_status = "not running"
else:
print("No hit, starting over")
示例输出:
Current shot is: 51
Current touch is: 33
No hit, starting over
Current shot is: 0
Current touch is: 81
No hit, starting over
Current shot is: 84
Current touch is: 9
No hit, starting over
Current shot is: 29
Current touch is: 56
No hit, starting over
Current shot is: 91
Current touch is: 29
No hit, starting over
Current shot is: 92
Current touch is: 37
No hit, starting over
Current shot is: 71
Current touch is: 61
HIT (- 10)