2016-01-20 2 views
0

Это многофакторный код, поэтому все его части понадобятся для его запуска, так же как и код, который предназначен для выбора 2 pokemon, формирующих текстовый файл и имеющих пользователя и компьютер выберите тип атаки.Имейте проблемы с проверкой ошибок и циклов

Проблема, с которой я сталкиваюсь, заключается в том, что функция битвы зацикливается, а затем предотвращает обман путем проверки ошибок, чтобы убедиться, что была применена действительная атака и что была введена только строка.

Я не прошу вас утончаются сделать всю работу за меня, но я застрял и не знаю, что еще TODO так что любая помощь будет принята с благодарностью

import random 
from fileopenpoke import select_random 

f = open('pokemon.txt', 'r',) 
l = f.read().splitlines() 

# important things 
# the attacks 
Attack = ["Air", "Water", "Grass"] 

# Score/attack 
def poke_score(): 
    score = random.choice(Attack) 
    return score 

# things 
user_poke = select_random(l) 
enemy_poke = select_random(l) 
enemy_score = poke_score() 

# staging 
print ("5 matches Best 3 out of 5") 
print user_poke, "Vs.", enemy_poke 
print ("Select Attack") 

# user select attack 
def make_score(): 
    score = raw_input("Air Water or Grass") 
    return score 
user_score = make_score() 
# error checking 
# output and battle sequence 
def battle(): 
    global user_score, enemy_score 
    etal = 0 
    utal = 0 
    match = 0 
# forfeits match if no attack or incorrect attack is given 

    if user_score == "Air" and enemy_score == "Grass": 
     etal = etal + 1 
     match = match + 1 
     print enemy_poke, "used", enemy_score, user_poke, "used", user_score, enemy_poke, "Match Won!" 
     print "current score", utal, "/", etal 
     print "match", match 
     return etal, match 

    elif user_score == "Grass" and enemy_score == "Water": 
     etal = etal + 1 
     match = match + 1 
     print enemy_poke, "used", enemy_score, user_poke, "used", user_score, enemy_poke, "Match Won!" 
     print "current score", utal, "/", etal 
     print "match", match 
     return etal, match 

    elif user_score == "Water" and enemy_score == "Air": 
     etal = etal + 1 
     match = match + 1 
     print enemy_poke, "used", enemy_score, user_poke, "used", user_score, enemy_poke, "Match Won!" 
     print "current score", utal, "/", etal 
     print "match", match 
     return etal, match 

    elif user_score == enemy_score: 
     match = match + 1 
     print enemy_poke, "used", enemy_score, user_poke, "used", user_score, "Match was a Tie!!" 
     print "match", match 
     return match 

    else: 
     utal = utal + 1 
     match = match + 1 
     print enemy_poke, "used", enemy_score, user_poke, "used", user_score, user_poke, "Match Won!" 
     print "current score", utal, "/", etal 
     print "match", match 
     return utal, match 


battle() 

это импорт файлов его маленький и не нужен, но его требуется для моего назначения

# pokemon list 
import random 
def select_random(l): 
    pokepick = random.choice(l) 
    return pokepick 

Это список Pokemon его довольно большой, так плохо просто связать его https://www.dropbox.com/s/trlv60u48rllfc0/pokemon.txt?dl=0

ответ

0

Простой подход к проверке ошибок при вводе пользователя с циклом while. Например:

def make_score(): 
    while True: 
     score = raw_input('Air, Water, or Grass? ') 
     if score in Attack: break 
     else: 
      print 'Invalid selection.' 
    return score 

Это будет цикл для ввода, пока пользователь вводит строку, которая соответствует одному из элементов списка (в списке атак).

Смежные вопросы