Wow! You may be a beginner at programming in python, but you post shows a lot of maturity! Here are a couple of things right off the bat:
1) sys.stdout.write("Enter the first planet: ")
is generally equivalent to
print "Enter the first planet: "
so use print because it's easier to read and you can save import sys 'til you really need it.
2) You have done an awesome job of research string "methods" ( in python jargon, just about everything is an object of one "type" or another and methods are the functions that belong to that particulay type of object) as show by your use of:
x = sys.stdin.readline().capitalize()
Here you've asked an str type object to give you a capitalized copy of itself!
You can use:
print type(x)
to prove that you have an str type object.
sys.stdin.readline() and raw_input() both return str type objects.
if you type:
help(str)
you'll get all the methods and attributes of str type objects.
There are other ways, but I use this while I'm writing for quick reminders.
3) Once you have a string with only on word in it (no spaces, etc.), you can use str methods find out what kind of characters are in it:
print "1234".isdigit()
print "abcd".isdigit()
print "1a2b".isalpha()
If input_string.isdigit():
x = int(input_string)
etc.
If your string has spaces in it, you can get a list type object which hold the text between the spaces (list of str type objects) by using:
list_of_words = input_string.split() # There are arguments to split() like "\t", but that comes later.
4) The very cool python structure that you need to use to solve a "lookup" of text mapped to a value is the dictionary!
planet_dist_dict = {"earth":80000, "Mars":500000}
then you can
print planet_dist_dict.get("earth")
print planet_dist_dict.get("foo") # dict.get() handles non-existant items
x = planet_dist_dict["Mars"]
print x
or do things like
if input_string in planet_dist_dict:
# do stuff
5) That last line use more cool python syntax:
Lots of python's types a in a group iterators. Learning about these was key for me. Iterators let you do thing like look through the hole structure as if you had written a loop. That's what
in does.
6) Have fun! I am so glad that you get this opertunity in school. Python is my develpment language of choice for very many reasons, but mostly because there is almost never a time when python says "you can't do that".
Hi all, Ive been searching everywhere, and dont understand why there isnt that much infomation on "inputing" answers and having the program evaluate the answer.
So far ive found raw_input, input, and sys.stdout.write(" ") --> sys.stdin.readline() commands. Ive found these work with numbers, but what if the input I put in is text???
Anyway, I want a program that will ask the user a question, and is able to read and evaluate the answer if it consists of letters, or letters and numbers.
Heres a example of what ive been trying:
#part4
#purpose: compute the distance of two planets, ask the user which two planets to compute
import sys
#variables
Earth = 80000
Mars = 500000
Venus = 67000
Sun = 0
sys.stdout.write("Enter the first planet: ")
x = sys.stdin.readline().capitalize()
sys.stdout.write("Enter the second planet: ")
y = sys.stdin.readline().capitalize()
z = abs(x - y)
print "The distance of the two planets are: ", z
Anyway, I am a big beginner of python, and yes this is homework!! However, the example i have up here isnt the real problem, I made it up, any help would be really appreciated!!!