Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
def python错误重复_Python_Python 3.x_Function - Fatal编程技术网

def python错误重复

def python错误重复,python,python-3.x,function,Python,Python 3.x,Function,虽然我输入了一个数值,但它仍然给我一个错误。我不知道为什么会这样。。帮助某人 def is_string(s): rate = input(s) try: str.isalpha(rate) print('There was an error. Please try again. Make sure you use numerical values and alpabetical. ') return is_string(s) #

虽然我输入了一个数值,但它仍然给我一个错误。我不知道为什么会这样。。帮助某人

def is_string(s):
    rate = input(s)
    try:
        str.isalpha(rate)
        print('There was an error. Please try again. Make sure you use numerical values and alpabetical. ')
        return is_string(s)  #ask for input again
   except:
        return rate
ExRate = is_string('Please enter the exchange rate in the order of, 1 '+Currency1+' = '+Currency2)

def is_string2(msg):
    amount = input(msg)
    try:
        str.isalpha(amount)
        print('There was an error. Please try again. Make sure you use numerical values. ')
        return is_string2(msg)  #ask for input again
    except:
        return amount
Amount = is_string2('Please enter the amount you would like to convert:')

我不知道为什么要使用异常,而您应该使用just-if语句:

def is_string(s):
    rate = input(s)
    if str.isalpha(rate):
        print('There was an error. Please try again. Make sure you use numerical values and alpabetical. ')
        return is_string(s)  #ask for input again
    else:
        return rate
ExRate = is_string('Please enter the exchange rate in the order of, 1 '+Currency1+' = '+Currency2)

def is_string2(msg):
    amount = input(msg)
    if str.isalpha(amount):
        print('There was an error. Please try again. Make sure you use numerical values. ')
        return is_string2(msg)  #ask for input again
    else:
        return amount
Amount = is_string2('Please enter the amount you would like to convert:')

你在找这样的东西吗

def get_int(prompt, error_msg):
    while True:
        try:
            return int(input(prompt))
        except ValueError:
            print(error_msg)


rate = get_int(
    'Please enter the exchange rate in the order of, 1 {} = {}'
        .format(Currency1, Currency2),
    error_msg="Rate must be an integer")
amount = get_int(
    'Please enter the amount you would like to convert:',
    error_msg="Amount must be an integer")

您不应该使用try语句,我也不认为您应该使用isalpha()。isnumeric()测试数值的有效性。对于“%#-@”这样的字符串,isalpha()将返回false


您想在这里做什么?阅读文档,您可能会发现错误。您希望在
try
子句的哪一部分抛出错误?打印“有一个错误”不足以从
try
语句中中断。@Ben操作人员甚至没有这样做,因为他丢弃了结果,并期望它抛出异常,因为人类不知道原因,因为这不是方法所做的,也没有合理的理由期望它会这样做。@Jemini然后要做的事情是
try:amount=int(原始输入)(…)
如果有的话。我也会特别注意到
ValueError
。我尝试了if语句,但在我尝试创建货币转换器时,它对我的代码整体不起作用。感谢。-1修复了非Python括号。代码现在更加Python化了。
while True:
    s = input("Enter amount: ")
    if s.isnumeric():
        break
    print("There was a problem. Enter a number.")