我对python和一般的编程都是新手,我正在尝试写一个简单的游戏,使用随机生成的口袋妖怪,玩家和电脑的能力,谁拥有最高的能力谁就赢。
我面临的问题是,我不知道如何让分数更新后,每一轮没有重置回0。我希望游戏继续无限,直到球员输了,并增加了一个分数每一轮。
有人能帮忙吗?这是当前代码:
import random
import requests
def random_pokemon():
pokemon_number = random.randint(1, 151)
url = 'https://pokeapi.co/api/v2/pokemon/{}/'.format(pokemon_number)
response = requests.get(url)
pokemon = response.json()
return {
'name': pokemon['name'],
'Pokedex ID': pokemon['id'],
'Height': pokemon['height'],
'Weight': pokemon['weight'],
}
def random_move():
pokemon_move = random.randint(1,844)
url = 'https://pokeapi.co/api/v2/move/{}/'.format(pokemon_move)
response = requests.get(url)
move = response.json()
return {
'name': move['name'],
'power': move['pp'],
}
score = 0
def add_to_score():
global score
score =+ 1
def current_score():
global score
def run():
player_pokemon = random_pokemon()
print('Your Pokemon is {}'.format(player_pokemon['name']))
player_move = random_move()
print('Your move is {}'.format(player_move['name']))
opponent_pokemon = random_pokemon()
print('The opponents Pokemon is {}'.format(opponent_pokemon['name']))
opponent_move = random_move()
print('The opponents move is {}'.format(opponent_move['name']))
player_stat = player_move['power']
opponent_stat = opponent_move['power']
print('Your Stat is {}'.format(player_stat))
print('The opponents Stat is {}'.format(opponent_stat))
if player_stat > opponent_stat:
print('You Win! Congratulations!\n')
print('Your score is {}'.format(add_to_score()))
run()
elif player_stat < opponent_stat:
print('You Lose! Better Luck Next Time!')
elif player_stat == 'None':
print('You Lose! Better Luck Next Time!')
elif opponent_stat == 'None':
print('You Win! Congratulations!\n')
print('Your score is {}'.format(add_to_score()))
run()
else:
print('Its a Tie!\n')
print('Your score is {}'.format(current_score()))
run()
run()
我试过在run()函数中加入score函数,但结果还是一样。就像我说的,我对这一切都是全新的,所以如果有人能帮助我,我将不胜感激!
1条答案
按热度按时间ar7v8xwq1#
您的add_to_score函数存在打字错误,应为:
另外,当打印分数时,你不会从函数返回任何东西,这就是为什么你会得到None。我建议直接打印分数变量。
完整代码: