Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
88 views
in Technique[技术] by (71.8m points)

python - Variable name not found

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Firstly, since you are using python, you don't have to add ; at the end of each statement.

Now, by default doing input('') returns a string. You have to change the type to float for weight & int for planet

print("I have information for the following planets:
")
print("   1. Venus   2. Mars    3. Jupiter")
print("   4. Saturn  5. Uranus  6. Neptune
")
 
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")

In the last print statement, you have to convert the type of weight back to string for printing out the statement


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...