Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.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_Variables_Python 3.x_Variable Assignment - Fatal编程技术网

Python 是什么导致了这个程序中的错误

Python 是什么导致了这个程序中的错误,python,variables,python-3.x,variable-assignment,Python,Variables,Python 3.x,Variable Assignment,我做的一个程序有点麻烦。我不太清楚问题出在哪里。然而,我想不出要寻找什么来解决这个问题。既然如此,如果这是一个重复的问题,我提前道歉 # convert.py # A program to convert Celsius temps to Fahrenheit def main(): print("Hello", end=" ") print("this program will convert any 5 different celsius temperatures to f

我做的一个程序有点麻烦。我不太清楚问题出在哪里。然而,我想不出要寻找什么来解决这个问题。既然如此,如果这是一个重复的问题,我提前道歉

# convert.py
# A program to convert Celsius temps to Fahrenheit

def main():
    print("Hello", end=" ")
    print("this program will convert any 5 different celsius temperatures to fahrenheit.")
    c1, c2, c3, c4, c5 = eval(input("Please enter 5 different celsius temperatures seperated by commas: "))
    print(c1, c2, c3, c4, c5)
    for i in range(5):
        c = ("c" + str(i + 1))
        print(c)
        fahrenheit = 9/5 * c + 32
        print("The temperature is", fahrenheit, "degrees Fahrenheit.")
    input("The program has now finished press enter when done: ")

main()

该程序在第一个循环上的华氏赋值语句之前工作正常。我确信问题涉及变量以及我分配变量的最可能的错误方式。因此,如果有人能指出我做错了什么以及为什么它不起作用,我将不胜感激。

非常接近,但不要转换为字符串:

def main():
    print("Hello", end=" ")
    print("this program will convert any 5 different celsius temperatures to fahrenheit.")
    temps = eval(input("Please enter 5 different celsius temperatures seperated by commas: "))
    print(*temps)
    for c in temps:
        print(c)
        fahrenheit = 9/5 * c + 32
        print("The temperature is", fahrenheit, "degrees Fahrenheit.")
    input("The program has now finished press enter when done: ")

main()
不建议使用
eval
,因为用户可以执行任意Python代码。最好明确地转换数字:

prompt = "Please enter 5 different celsius temperatures seperated by commas: "
temps = [int(x) for x in input(prompt).split(',')]
这:

创建字符串
'c1'
'c2'
等等。它们不同于使用
input
在行中指定的名称
c1
c2
。更容易将用户输入的所有值放入
temp
。一、二、十、一百都无所谓。Python允许直接使用以下命令循环
temps

for c in temps:

在这里,
c
依次成为存储在
temps

中的每个数字,100次中有99次,如果您试图动态访问变量,那么您就做错了。将输入保持为元组(
temperatures=eval(input(…)
)并对其进行迭代(
以温度表示的温度:
)谢谢您的帮助。我对编程仍然非常陌生,不知道什么可以做,什么不能做。欢迎来到这里。你的尝试已经相当不错了。我们都在这里学习。:)
for c in temps: