2016-01-20 29 views
0

這是一個多文件代碼,所以所有的部分將需要運行它,代碼的工作原理是選擇2 pokemon形式的文本文件,並有用戶和計算機選擇攻擊類型。即時通訊有錯誤檢查和循環

我遇到的問題是讓戰鬥功能循環,然後通過錯誤檢查防止作弊,以確保使用了有效的攻擊並且只輸入了str。

我不是找你要全力以赴做好各項工作,爲我,但我是被卡住,不知道待辦事項所以任何幫助會還有什麼不勝感激

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 

這是寵物小精靈列表中的相當大如此虐待只是掛靠 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 

這將循環輸入,直到用戶輸入一個符合您的列表項目之一(在攻擊列表)的字符串。

相關問題