简体   繁体   English

从我制作的另一个脚本中以python导入函数

[英]importing functions in python from another script I made

I'm kind of stuck at a problem where my teacher wants me to import functions from another script 我有点陷入一个问题,就是我的老师要我从另一个脚本导入函数

def readint():
    prompt = int(input("Enter an integer: "))
    print(" You entered: ",prompt," and the type is", type(prompt))

and then on the other program I can get it to import like 然后在另一个程序上,我可以像导入一样

import test 

test.readint()

but when I try to get it exactly how she wants 但是当我尝试完全按照她的意愿

test.readint(prompt)

I can't seem to get it to work. 我似乎无法正常工作。 I tried this but it doesn't seem to work 我尝试了这个,但似乎没有用

def readint(prompt):
prompt = int(input("Enter an integer: "))
print(" You entered: ",prompt," and the type is", type(prompt))
return prompt

any explanation would be greatly appreciated! 任何解释将不胜感激!

You may be having problems with the commas in the print statement. 打印声明中的逗号可能有问题。

When I ran your initial program, I got this: 当我运行您的初始程序时,我得到了:

>>> 
Enter an integer: 3
(' You entered: ', 3, ' and the type is', <type 'int'>)

Which doesn't seem to be like what you wanted (it's a tuple) 似乎不像您想要的(这是一个元组)

What you seem to want to print is the string, 您似乎要打印的是字符串,

You entered: 3 and the type is <type 'int'>

To do this, you need to cast your variables to strings. 为此,您需要将变量转换为字符串。


test.py test.py

def readint(prompt):
    prompt = int(input("Enter an integer: "))
    print ("You entered: " + str(prompt) + " and the type is " + str(type(prompt)))
    return prompt

main.py main.py

import temp

prompt = None
temp.readint(prompt)

Note the + operators and str casts 注意+运算符和str强制转换


For me, this outputs the following: 对我来说,这输出以下内容:

>>> 
Enter an integer: 3
You entered: 3 and the type is <type 'int'>

Which looks to be what you are looking for. 看起来就是您要找的东西。


As a side note, I'm not sure why you want to pass in the prompt variable because it can be left out and there is no reason to have it in there. 附带说明一下,我不确定为什么要传入prompt变量,因为可以将其忽略,也没有理由将其放入其中。 Simply removing it from the function definition and not passing it in is perfectly valid: 只需将其从函数定义中删除而不将其传递进来就完全有效:

def readint():
    ...

and

import test
test.readint()

Happy Coding! 编码愉快!

--Elipzer --Elipzer

First off, instead of printing the result to the screen when you get the value of the prompt, try returning it as a string. 首先,不要在获取提示值时将结果打印到屏幕上,而是尝试将其作为字符串返回。 That way, you can assign the return value to a variable and access it later. 这样,您可以将返回值分配给变量并在以后访问它。 Pretty neat, huh? 很整洁吧? Your second attempt already had it, but there were two little problems: 您的第二次尝试已经有了,但是有两个小问题:

There's no point in passing an argument to this function. 向该函数传递参数毫无意义。 This is because input() already stores a value in a variable, so there'd be no point in passing an argument, you could just create a function variable and store it in there. 这是因为input()已经在变量中存储了一个值,所以传递参数毫无意义,您可以创建一个函数变量并将其存储在其中。 Also, there'd be no point in doing " and the type is",type(prompt)) . 另外,执行" and the type is",type(prompt))毫无意义。 The reason for this is because we already know it's an integer: you converted the result of prompt to an integer after you got it's string value. 这样做的原因是因为我们已经知道它是一个整数:在获得prompt的字符串值后,将prompt结果转换为整数。 If you passed a value to prompt that couldn't be converted to integer, you'd get a ValueError . 如果您传递了一个值以提示无法转换为整数,则将出现ValueError So, try using this code: 因此,请尝试使用以下代码:

def readint():
    prompt = int(input("Enter an integer: "))
    return "You entered " + str(prompt)

That way, in your other file you can do this: 这样,在另一个文件中,您可以执行以下操作:

>>> import test
>>> a = test.readint()
Enter an integer7
>>> a
'You entered 7'
>>> 

(Keep in mind that the three > symbols aren't python code, it comes from the shell) If you were to enter a invalid value to be converted to an integer, it'd raise a ValueError . (请记住,三个>符号不是python代码,它来自外壳程序)如果要输入无效值以转换为整数,则会引发ValueError How can we prevent this? 我们如何防止这种情况? You can use a try , except statement. 您可以使用tryexcept声明except Basically, what this does, is it tries to do something, then, if an exception is raised during that process, it does something else. 基本上,这是在尝试执行某些操作,然后,如果在该过程中引发了异常,则会执行其他操作。

So, all in all. 因此,总而言之。 Your code would look like this: 您的代码如下所示:

def readint():
    try:
        prompt = int(input("Enter an integer: "))
    except ValueError:
        return "Invalid value! It must be a number."
    return "You entered " + str(prompt)

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

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