简体   繁体   English

你如何使用输入函数和 def 函数?

[英]How do you use input function along with def function?

I'm new to programming.我是编程新手。 I have a bit of problem with python coding (with def function).我对 python 编码(使用 def 函数)有点问题。

So basically the code has to give the smaller number out of 2 numbers.所以基本上代码必须给出2个数字中较小的数字。

Question: Write codes to perform the following tasks:问题:编写代码以执行以下任务:

  1. Define a function smaller_num that takes in two numbers to determine and return the smaller number of the two.定义一个函数smaller_num,它接受两个数字来确定并返回两者中较小的数字。
    Ask user for two numbers向用户询问两个号码
    Use the function to determine the smaller number and display the result使用函数确定较小的数字并显示结果

So I had user input instead of calling the function and adding value inside the variable.所以我有用户输入,而不是调用函数并在变量中添加值。

This is how my code looks like:这就是我的代码的样子:

def smaller_num(x,y):
    if x>y:
        number= y
    else:
        number= x
    return number

smaller_num(x= input("Enter first number:-") ,y= input("Enter second number:-"))

print("The smaller number between " +  str(x) + " and " + str(y) + " is " + str(smaller_num))

How do I correct it?我该如何纠正? For now it's not working because "x" is not defined.现在它不起作用,因为“x”没有定义。 But I feel that I defined them clearly both in def function and input function.但是我觉得我在def函数和输入函数中都定义得很清楚。 So how do I fix this?那么我该如何解决呢?

Thanks for those who respond to this question.感谢那些回答这个问题的人。

You never actually defined x and y globally.您实际上从未在全局范围内定义过xy You only defined it in the function when you did def smaller_num(x, y) .只有在执行def smaller_num(x, y)时才在函数中定义它。

When you do smaller_num(x= input("Enter first number:-") ,y= input("Enter second number:-")) , you aren't creating variables called x and y , you are just creating parameters for your function.当您执行smaller_num(x= input("Enter first number:-") ,y= input("Enter second number:-")) ,您不是在创建名为xy变量,您只是在为您的功能。

In order to fix your code, create the variable x and y before you call your function:为了修复您的代码,请在调用函数之前创建变量xy

def smaller_num(x, y): ## Can be rephrased to  def smaller_num(x, y):
    if x > y:          ##                          if x > y:
        number = y     ##                              return y
    else:              ##                          else:
        number = x     ##                              return x
return number

x = input("Enter first number:-")
y = input("Enter second number:-")
result = smaller_num(x, y)
print("The smaller number between " +  str(x) + " and " + str(y) + " is " + str(result))

The other reason your code is not working is because you're not assigning the returned value of the function back into a variable.您的代码不起作用的另一个原因是您没有将函数的返回值分配回变量。 When you return something from a function, and again when you call the function, you need to assign the value to a variable, like I have: result = smaller_num(x, y) .当您从函数return某些内容时,再次调用该函数时,您需要将该值分配给一个变量,就像我有的: result = smaller_num(x, y)

When you called your function, you never assigned the value to a variable, so it has been wasted.当您调用函数时,您从未将值分配给变量,因此它被浪费了。


Also, are you using Python 3 or 2.7?另外,您使用的是 Python 3 还是 2.7? In python 3 using input() will return a string, and to convert this to an integer, you can call int() around the input() function.在 python 3 中使用input()将返回一个字符串,并将其转换为整数,您可以在input()函数周围调用int()

This will work:这将起作用:

def smaller_num(x,y):

    if x>y:
        number= y
    else:
        number= x
    return number


x = input("Enter first number:-")

y = input("Enter second number:-")

smaller = smaller_num(x,y)

print("The smaller number between " +  str(x) + " and " + str(y) + " is " + str(smaller))

this should work:这应该有效:

def smaller_num(x,y):
    if x>y:
        number = y
    else:
        number = x
    return number

x = input("Enter first number:-")
y = input("Enter second number:-")

print("The smaller number between " +  str(x) + " and " + str(y) + " is " + str(smaller_num(x,y)))
def smaller_num(x,y):
    if x>y:
        number= y
    else:
        number= x
    return number

k=smaller_num(x= input("Enter first number:-") ,y= input("Enter second number:-"))

print("The smaller number between x  & y  is ", k)

 def smaller_num(x,y): if x>=y: number = y else: number = x return number x = input("Enter first number:-") y = input("Enter second number:-") print("The smaller number between " + str(x) + " and " + str(y) + " is " + str(smaller_num(x,y)))

def smaller_num():
    x=int(input('Enter the first number: '))
    y=int(input('Enter the second number: '))
    if x<y:
        return x
    else:
        return y
print('The smaller number is: ',smaller_num())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM