【问题标题】:Repeating the same processes for different variables [closed]对不同的变量重复相同的过程[关闭]
【发布时间】:2014-02-11 21:15:20
【问题描述】:

我正在编写代码,我必须输入战士和敌人的力量和技能。是否有任何有效的方法来编写必须以相同方式工作的程序,例如检查每个输入不是.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()

【问题讨论】:

标签: python validation python-2.7 input


【解决方案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))

【讨论】:

  • 我得到了 python 2.7.5,它似乎工作它只是打印出什么都没有,没有任何错误。你能输入它为 fighter_strength、fighter_skill 和enemy_strength 和enemy_skill 请尽快回复我需要这个GCSE。
  • 抱歉,我现在已经纠正了一个逻辑错误(检查 min_max_ 值)。但是,当我运行它时,它肯定会出错。
  • stackoverflow.com/help/someone-answers(但谢谢,很乐意提供帮助!)
  • 我在另一个代码中遇到问题,当我输入一个负值时它会拒绝它,但是这个以某种方式接受它
  • Chears 我知道了将最小值更改为零
猜你喜欢
  • 2021-09-21
  • 2021-05-18
  • 1970-01-01
  • 1970-01-01
  • 2022-08-13
  • 1970-01-01
  • 1970-01-01
  • 2012-07-07
  • 1970-01-01
相关资源
最近更新 更多