So I created a program which assigns a string value to a variable.
Here is the code
print("I have information for the following planets:
");
print(" 1. Venus 2. Mars 3. Jupiter");
print(" 4. Saturn 5. Uranus 6. Neptune
");
weight = input("Enter your weight: ");
planet = 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: " + weight + "kg");
So when I try to run the program it says planet_name
was not found and also prints else statement
Here is the output with errors
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
I am unable to find what's causing it.
question from:
https://stackoverflow.com/questions/65938792/variable-name-not-found 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…