以字符串形式运行的代码需要将其转换为float

时间:2019-04-18 12:11:09

标签: python-3.x

我只需要帮助将用户的输入转换为float thats

谢谢

$(function () {

    $('.btnNext').click(function (e) {
        if ($("#form1").valid()) {
            alert('valid');
        } else {
            alert('not valid');
        }
    });

    $("#form1").validate({
        rules: {
            PublicationName: {
                required: true,
                minlength: 2
            }
        },
        messages: {
            PublicationName: {
                required: "Please enter a Publication Name",
                minlength: "Your username must consist of at least 2 characters"
            }
        },
        errorElement: "em",
        errorPlacement: function (error, element) {
            // Add the `help-block` class to the error element
            error.addClass("help-block");

            // Add `has-feedback` class to the parent div.form-group
            // in order to add icons to inputs
            element.parents(".col-sm-5").addClass("has-feedback");

            if (element.prop("type") === "checkbox") {
                error.insertAfter(element.parent("label"));
            } else {
                error.insertAfter(element);
            }

            // Add the span element, if doesn't exists, and apply the icon classes to it.
            if (!element.next("span")[0]) {
                $("<span class='glyphicon glyphicon-remove form-control-feedback'></span>").insertAfter(element);
            }
        },
        success: function (label, element) {
            // Add the span element, if doesn't exists, and apply the icon classes to it.
            if (!$(element).next("span")[0]) {
                $("<span class='glyphicon glyphicon-ok form-control-feedback'></span>").insertAfter($(element));
            }
        },
        highlight: function (element, errorClass, validClass) {
            $(element).parents(".col-sm-5").addClass("has-error").removeClass("has-success");
            $(element).next("span").addClass("glyphicon-remove").removeClass("glyphicon-ok");
        },
        unhighlight: function (element, errorClass, validClass) {
            $(element).parents(".col-sm-5").addClass("has-success").removeClass("has-error");
            $(element).next("span").addClass("glyphicon-ok").removeClass("glyphicon-remove");
        }
    });

});

2 个答案:

答案 0 :(得分:0)

def takenum(x,y):
    print("Your first number is " + x + " your second number is " + y)
    result = (float(x) + float(y))
    print(result)

x = input("put your first number: " )
y = input("Put your second number: " )

takenum(x, y)

enter image description here

  

注意:您的输入应为3.5而不是3,6

     

使用点作为小数点分隔符

或者,如果您要使用点和逗号或仅使用逗号作为分隔符, 只需用replace()删除,(逗号):

def takenum(x,y):
    print("Your first number is " + x + " your second number is " + y)
    result = (float((x).replace(',','.')) + float((y).replace(',','.')))
    print(result)

x = input("put your first number: " )
y = input("Put your second number: " )

takenum(x, y)

代码在做什么:

float((x).replace(',','.')) 

您有x作为字符串(输入是返回字符串),将(逗号)替换为(点),并将字符串转换为浮点型

enter image description here

答案 1 :(得分:0)

使用WARN [Controller id=2, targetBrokerId=2] Connection to node 2 (localhost/127.0.0.1:9093) could not be established. Broker may not be available. (org.apache.kafka.clients.NetworkClient) WARN [Controller id=2, targetBrokerId=3] Connection to node 3 (localhost/127.0.0.1:9094) could not be established. Broker may not be available. (org.apache.kafka.clients.NetworkClient) 读取输出时,可以将输出显式转换为浮点数,然后使用字符串格式进行打印。

float(input())

您的输出将如下所示:

def takenum(x,y):
    print("Your first number is {} and your second number is {}".format(x, y))
    result = x + y
    print(result)

x = float(input("put your first number: " ))
y = float(input("Put your second number: " ))

takenum(x, y)