Python 找不到变量名
所以我创建了一个程序,它给一个变量分配一个字符串值 这是密码Python 找不到变量名,python,python-3.x,Python,Python 3.x,所以我创建了一个程序,它给一个变量分配一个字符串值 这是密码 print(“我有以下行星的信息:\n”); 印刷品(“1.金星2.火星3.木星”); 印刷体(“4.土星5.天王星6.海王星”); 重量=输入(“输入您的重量:”); 行星=输入(“输入行星编号:”); #在下面写一个if语句: 如果行星==1: 行星名称=‘金星’; 重量=重量*0.91; elif planet==2: 行星名称='火星'; 重量=重量*0.38; elif planet==3: 行星名称='木星'; 重量=重量
print(“我有以下行星的信息:\n”);
印刷品(“1.金星2.火星3.木星”);
印刷体(“4.土星5.天王星6.海王星”);
重量=输入(“输入您的重量:”);
行星=输入(“输入行星编号:”);
#在下面写一个if语句:
如果行星==1:
行星名称=‘金星’;
重量=重量*0.91;
elif planet==2:
行星名称='火星';
重量=重量*0.38;
elif planet==3:
行星名称='木星';
重量=重量*2.34;
elif planet==4:
行星名称='土星';
重量=重量*1.06;
elif planet==5:
行星名称='天王星';
重量=重量*0.92;
elif planet==6:
行星名称=‘海王星’;
重量=重量*1.19;
其他:
打印(“未找到行星”);
打印(“您在“+planet_name+”上的体重为:“+weight+”kg”);
因此,当我试图运行该程序时,它会说找不到planet\u name
,并打印else语句
这是带有错误的输出
I have information for the following planets:
1. Venus 2. Mars 3. Jupiter
4. Saturn 5. Uranus 6. Neptune
Enter your weight: 55
Enter planet number: 6
Planet Not Found
Traceback (most recent call last):
File "/data/data/com.termux/files/home/python/planet.py", line 28, in <module>
print("Your weight on " + planet_name + " is: " + weight + "kg");
NameError: name 'planet_name' is not defined
我有以下行星的信息:
1.金星2号。火星3号。木星
4.土星5号。天王星6。海王星
输入您的体重:55
输入行星编号:6
找不到行星
回溯(最近一次呼叫最后一次):
文件“/data/data/com.termux/files/home/python/planet.py”,第28行,在
打印(“您在“+planet_name+”上的体重为:“+weight+”kg”);
名称错误:未定义名称“行星名称”
我无法找到原因。您需要将
planet\u name
默认为空字符串,并正确地投射weight
和planet
,例如:
print("I have information for the following planets:\n");
print(" 1. Venus 2. Mars 3. Jupiter");
print(" 4. Saturn 5. Uranus 6. Neptune\n");
weight = float(input("Enter your weight: "));
planet = int(input("Enter planet number: "));
# Write an if statement below:
if planet == 1:
planet_name = 'Venus';
weight = weight * 0.91;
elif planet == 2:
planet_name = 'Mars';
weight = weight * 0.38;
elif planet == 3:
planet_name = 'Jupiter';
weight = weight * 2.34;
elif planet == 4:
planet_name = 'Saturn';
weight = weight * 1.06;
elif planet == 5:
planet_name = 'Uranus';
weight = weight * 0.92;
elif planet == 6:
planet_name = 'Neptune';
weight = weight * 1.19;
else:
planet_name = ''
print("Planet Not Found");
print("Your weight on " + planet_name + " is: " + str(weight) + "kg");
->
您需要将
planet\u name
默认为空字符串,并正确地投射weight
和planet
,例如:
print("I have information for the following planets:\n");
print(" 1. Venus 2. Mars 3. Jupiter");
print(" 4. Saturn 5. Uranus 6. Neptune\n");
weight = float(input("Enter your weight: "));
planet = int(input("Enter planet number: "));
# Write an if statement below:
if planet == 1:
planet_name = 'Venus';
weight = weight * 0.91;
elif planet == 2:
planet_name = 'Mars';
weight = weight * 0.38;
elif planet == 3:
planet_name = 'Jupiter';
weight = weight * 2.34;
elif planet == 4:
planet_name = 'Saturn';
weight = weight * 1.06;
elif planet == 5:
planet_name = 'Uranus';
weight = weight * 0.92;
elif planet == 6:
planet_name = 'Neptune';
weight = weight * 1.19;
else:
planet_name = ''
print("Planet Not Found");
print("Your weight on " + planet_name + " is: " + str(weight) + "kg");
->
首先,因为您使用的是python,所以不必添加
在每条语句的结尾处添加代码>
现在,默认情况下,执行输入(“”)
返回一个字符串。对于weight
,int
对于planet
print("I have information for the following planets:\n")
print(" 1. Venus 2. Mars 3. Jupiter")
print(" 4. Saturn 5. Uranus 6. Neptune\n")
weight = float(input("Enter your weight: "))
planet = int(input("Enter planet number: "))
# Write an if statement below:
if planet == 1:
planet_name = 'Venus'
weight = weight * 0.91
elif planet == 2:
planet_name = 'Mars'
weight = weight * 0.38
elif planet == 3:
planet_name = 'Jupiter'
weight = weight * 2.34
elif planet == 4:
planet_name = 'Saturn'
weight = weight * 1.06
elif planet == 5:
planet_name = 'Uranus'
weight = weight * 0.92
elif planet == 6:
planet_name = 'Neptune'
weight = weight * 1.19
else:
print("Planet Not Found")
print("Your weight on " + planet_name + " is: " + str(weight) + "kg")
在最后一个print
语句中,您必须将weight
的类型转换回string
,以便打印出该语句。首先,由于您使用的是python,因此不必添加在每条语句的结尾处添加代码>
现在,默认情况下,执行输入(“”)
返回一个字符串。对于weight
,int
对于planet
print("I have information for the following planets:\n")
print(" 1. Venus 2. Mars 3. Jupiter")
print(" 4. Saturn 5. Uranus 6. Neptune\n")
weight = float(input("Enter your weight: "))
planet = int(input("Enter planet number: "))
# Write an if statement below:
if planet == 1:
planet_name = 'Venus'
weight = weight * 0.91
elif planet == 2:
planet_name = 'Mars'
weight = weight * 0.38
elif planet == 3:
planet_name = 'Jupiter'
weight = weight * 2.34
elif planet == 4:
planet_name = 'Saturn'
weight = weight * 1.06
elif planet == 5:
planet_name = 'Uranus'
weight = weight * 0.92
elif planet == 6:
planet_name = 'Neptune'
weight = weight * 1.19
else:
print("Planet Not Found")
print("Your weight on " + planet_name + " is: " + str(weight) + "kg")
在最后一个print
语句中,您必须将weight
的类型转换回string
,以便在Python 3中打印出该语句,input
返回一个字符串并'1'!=1
必须将字符串转换为整数:
planet = int(input("Enter planet number: "))
或将值与字符串进行比较:
if planet == '1':
...
您还应该将weight
转换为float
..在Python 3中,input
返回一个字符串并'1'!=1
必须将字符串转换为整数:
planet = int(input("Enter planet number: "))
或将值与字符串进行比较:
if planet == '1':
...
您还应该将weight
转换为float
。将默认值移动到else
感谢您的帮助将默认值移动到else
感谢您的帮助它起作用了。感谢它真的起作用了。感谢它真的很有帮助谢谢您的帮助谢谢您的帮助