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
Python 无法将用户输入传递到函数中_Python_Python 3.x - Fatal编程技术网

Python 无法将用户输入传递到函数中

Python 无法将用户输入传递到函数中,python,python-3.x,Python,Python 3.x,你好:)我是python新手。我被要求构建一个脚本,将我们的度量值转换为公制度量值,反之亦然。应该很简单,但是当我运行脚本时,我得到了一个错误 "/home/oily/public_html/script.py Enter us or international for conversion:us Traceback (most recent call last): File "/home/oily/public_html/script.py", line 4,

你好:)我是python新手。我被要求构建一个脚本,将我们的度量值转换为公制度量值,反之亦然。应该很简单,但是当我运行脚本时,我得到了一个错误

"/home/oily/public_html/script.py
Enter us or international for conversion:us
Traceback (most recent call last):
  File "/home/oily/public_html/script.py", line 4, in <module>
    begin = input("Enter us or international for conversion:")
  File "<string>", line 1, in <module>
NameError: name 'us' is not defined" 

只需要一个函数,需要将变量
us
international
更改为
float

begin = input("Enter 'us' or 'international' for conversion:")

def start():
    if begin == "us":
        us = float(input("Please enter the value in international:"))
        print(us * 0.0348)
    else:
        international = float(input ("Please enter the value in us: "))
        print(international * 3.28084)

start()

这有很多错误。为什么要导入begin?如何运行Python脚本?我读了一些关于将用户输入传递到函数的文章,其中一些文章说需要在其中注册变量。我现在只是进入循环,所以我为我的无知道歉。我通过命令行运行它。老实说,这有太多的漏洞。我真的建议一个教程
return international
依赖于全局查找,
print(conversion)
只打印函数签名(不调用它),
import begin
只是。。。已断开,
返回int*3.28084
正试图与内置函数相乘。请花点时间学习教程。我只是好奇,为什么它没有重新确认我通过函数传递的变量?ELI5?仍然没有重新确认begin变量“us”未定义错误的原因是,在脚本开始时,您需要将us和国际变量定义为None。因此,上面的答案在脚本开头的us=None和international=None时有效
begin = input("Enter 'us' or 'international' for conversion:")

def start():
    if begin == "us":
        us = float(input("Please enter the value in international:"))
        print(us * 0.0348)
    else:
        international = float(input ("Please enter the value in us: "))
        print(international * 3.28084)

start()