为不同的变量重复相同的过程

时间:2014-02-11 21:15:20

标签: python validation python-2.7 input

我正在编写代码,我必须输入战斗机和敌人的力量和技能。是否有任何有效的方法来编写程序,它必须以相同的方式工作,例如当它再次要求输入时,检查每个输入不是.isdigit或超过1,000,000。

import random
import math

enemy_name="fierce dragon"
global enemy_name
fighter_name="undefeatable worrior"


def again_fighter_strength():
    global fighter_strength
    fighter_strength=raw_input("Please enter the strength of "+fighter_name+"(must 1M or below):")
    if fighter_strength.isdigit():
        fighter_strength=int(fighter_strength)
        if fighter_strength < 1000001:
            again_fighter_skill()
        else:
            print "Fighter strength must 1M or below"
            again_fighter_strength()
    else:
        print "Fighter strength must be a whole number."
        again_fighter_strength()


def again_fighter_skill():
    global fighter_skill
    fighter_skill=raw_input("Please enter the skill of your character(must 1M or below):")
    if fighter_skill.isdigit():
        fighter_skill=int(fighter_skill)
        if fighter_skill < 1000001:
            print""
            again_enemy_strength()
        else:
            print "Fighter Skill must 1M or below."
            again_fighter_skill()
    else:
        print "Fighter skill must be a whole number"
        again_fighter_skill()


def again_enemy_strength():
    global enemy_strength
    enemy_strength=raw_input("Please enter the strength of the "+enemy_name+"(must 1M or below):")
    if enemy_strength.isdigit():
        enemy_strength=int(enemy_strength)
        if enemy_strength < 1000001:
            again_enemy_skill()
        else:
            print enemy_name,"Enemy strength must 1M or below."
            again_enemy_strength()
    else:
        print "Enemy strength must be a whole number"
        again_enemy_strength()


def again_enemy_skill():
    global enemy_skill
    enemy_skill=raw_input("Please enter the skill of the "+enemy_name+"(must 1M or below):")
    if enemy_skill.isdigit():
        enemy_skill=int(enemy_skill)
        if enemy_skill < 1000001:
            print "your good to go"
        else:
            print enemy_name,"Fighter skill must be a whole number"
            again_enemy_skill()
    else:
        print "Fighter Skill must 1M or below"
        again_enemy_skill()

again_fighter_strength()

1 个答案:

答案 0 :(得分:1)

您可以将用户输入转换为独立功能:

def get_int(prompt, min_=None, max_=1000000):
    """Gets a user input integer value between min_ and max_ (inclusive)."""
    while True:
        try:
            ui = int(raw_input(prompt))
        except ValueError:
            print("Input must be an integer.")
        else:
            if max_ is not None and ui > max_:
                print("Input must be {0:,} or less.".format(max_))
            elif min_ is not None and ui < min_:
                print("Input must be {0:,} or more.".format(min_))
            else:
                return ui

这将无限循环,直到用户输入可接受的输入。你可以打电话,例如

fighter_strength = get_int("Please enter the strength of {0}.".format(fighter_name))
相关问题