【问题标题】:The function takes two arguments: Prompt: str and Return types: int, float, str该函数有两个参数:提示:str 和返回类型:int、float、str
【发布时间】:2013-02-17 22:28:54
【问题描述】:

大家好,这个问题我需要帮助

编写一个名为safe_input(提示,类型)的函数 像 Python 输入函数一样工作, 除了它只接受指定类型的输入。

该函数有两个参数:

提示:str

类型:int、float、str

函数会一直提示输入,直到指定类型的正确输入 进入。该函数返回输入。如果输入指定为数字(float 或 int),则返回的值将是正确的类型;也就是说,该函数将执行转换。

提示的默认值是空字符串。 类型的默认值是字符串。

这是我所拥有的:

safe_input = input(str("Enter a String Type you want to check: "))

test = safe_input("this is a string")
print ('"{}" is a {}'.format(test,type(test)))
test = safe_input("this is a string",int)
print ('"{}" is a {}'.format(test,type(test)))
test = safe_input("this is a string",float)
print ('"{}" is a {}'.format(test,type(test)))                       
test = safe_input(5)
print ('"{}" is a {}'.format(test,type(test)))
test = safe_input(5,int)
print ('"{}" is a {}'.format(test,type(test)))
test = safe_input(5,float)
print ('"{}" is a {}'.format(test,type(test)))
test = safe_input(5.044)
print ('"{}" is a {}'.format(test,type(test)))
test = safe_input(5.044, int)
print ('"{}" is a {}'.format(test,type(test)))
test = safe_input(5.044, float)
print ('"{}" is a {}'.format(test,type(test)))

def safe_input (prompt, type=str):

    if (type == int):
        while (True):
           try:
            # check for integer or float
            integer_check = int(prompt)
            # If integer, numbers will be equal
            if (prompt == integer_check):
                return integer_check
            else:
                print("They are not equal!!")
                return integer_check
        except ValueError:
            print ("Your entry, {}, is not of {}."
                   .format(prompt,type))
            prompt = input("Please enter a variable of the type '{}': "
                           .format(type))

有人知道我在这里做错了什么吗?我和我的朋友已经为此工作了好几个小时。

更新:我收到如下错误:

  File "C:\Users\Thomas\Desktop\ei8069_Lab9_Q4.py", line 28, in <module>
       test = safe_input("this is a string")
  TypeError: 'int' object is not callable

  Traceback (most recent call last):
  File "C:\Users\Thomas\Desktop\ei8069_Lab9_Q4.py", line 28, in <module>
       test = safe_input("this is a string")
  TypeError: 'float' object is not callable

【问题讨论】:

  • 您的代码目前有什么问题? (它表现出什么不正确的行为?)
  • 在您的第一行中,您将 safe_input 的值分配给用户输入的值,然后像调用函数一样调用它!先定义safe_input函数,input()调用应该在函数内部。

标签: python prompt


【解决方案1】:

只要我的两分钱。

首先,更仔细地阅读你的作业,你的方法并不是他们想要的。

而且你使用了很多不必要的条件。您的函数可能会更简单,如下所示:

def safe_input(prompt, type_=str):
    if(type_ not in (str, int, float)): 
        raise ValueError("Expected str, int or float.")  

    while True:
        test = input(prompt)    
        try:
            ret = type_(test)
        except ValueError:
            print("Invalid type, enter again.")                
        else:
            break    

    return ret

尽量不要使用像 type 这样的内置函数作为变量名。它们会覆盖内置函数,以后可能会引起很多麻烦。

【讨论】:

    【解决方案2】:

    为什么会有这行:

    safe_input = input(str("Enter a String Type you want to check: "))
    

    在程序开始时。你应该有函数的定义,但你有它在最后。

    解决方案:删除该虚假行并将函数的定义移至程序顶部。

    说明:当您运行该行时,它会向用户询问某些内容,并且(例如 42 将被分配给 safe_input。然后您尝试将其用作函数,但是,嘿,42 是一个整数,不能调用。

    【讨论】:

    • 我明白了,大多数情况下只是用户输入的位置并首先定义 safe_input 函数谢谢大家!!
    猜你喜欢
    • 2019-03-21
    • 2020-07-21
    • 2019-06-21
    • 2018-12-29
    • 2018-03-29
    • 2022-08-18
    • 2013-05-08
    • 1970-01-01
    相关资源
    最近更新 更多