Sign in to follow this  
biggjoee5790

Python problems

Recommended Posts

biggjoee5790    229
hi guys ive been using a python tutorial and its been going great. I was fine until i reached the input and variables section. heres the page that im doing: (everything inside the dotted lines is copied from the book im using) ------------------------------------------------------------------------------- Input and Variables Now I feel it is time for a really complicated program. Here it is: print "Halt!" s = raw_input("Who Goes there? ") print "You may pass,", s When I ran it here is what my screen showed: Halt! Who Goes there? Josh You may pass, Josh Of course when you run the program your screen will look different because of the raw_input statement. When you ran the program you probably noticed (you did run the program, right?) how you had to type in your name and then press Enter. Then the program printed out some more text and also your name. This is an example of input. The program reaches a certain point and then waits for the user to input some data that the program can use later. Of course, getting information from the user would be useless if we didn't have anywhere to put that information and this is where variables come in. In the previous program s is a variable. Variables are like a box that can store some piece of data. Here is a program to show examples of variables: a = 123.4 b23 = 'Spam' first_name = "Bill" b = 432 c = a + b print "a + b is", c print "first_name is", first_name print "Sorted Parts, After Midnight or", b23 And here is the output: a + b is 555.4 first_name is Bill Sorted Parts, After Midnight or Spam Variables store data. The variables in the above program are a, b23, first_name, b, and c. The two basic types are strings and numbers. Strings are a sequence of letters, numbers and other characters. In this example b23 and first_name are variables that are storing strings. Spam, Bill, a + b is, and first_name is are the strings in this program. The characters are surrounded by " or '. The other type of variables are numbers. Okay, so we have these boxes called variables and also data that can go into the variable. The computer will see a line like first_name = "Bill" and it reads it as "Put the string Bill into the box (or variable) first_name". Later on it sees the statement c = a + b and it reads it as "Put a + b or 123.4 + 432 or 555.4 into c". Here is another example of variable usage: a = 1 print a a = a + 1 print a a = a * 2 print a And of course here is the output: 1 2 4 Even if it is the same variable on both sides the computer still reads it as "First find out the data to store and then find out where the data goes". One more program before I end this chapter: num = input("Type in a Number: ") str = raw_input("Type in a String: ") print "num =", num print "num is a", type(num) print "num * 2 =", num * 2 print "str =", str print "str is a", type(str) print "str * 2 =", str * 2 The output I got was: Type in a Number: 12.34 Type in a String: Hello num = 12.34 num is a <type 'float'> num * 2 = 24.68 str = Hello str is a <type 'string'> str * 2 = HelloHello Notice that num was gotten with input while str was gotten with raw_input. raw_input returns a string while input returns a number. When you want the user to type in a number use input but if you want the user to type in a string use raw_input. The second half of the program uses type which tells what a variable is. Numbers are of type int or float (which are short for 'integer' and 'floating point' respectively). Strings are of type string. Integers and floats can be worked on by mathematical functions, strings cannot. Notice how when python multiples a number by an integer the expected thing happens. However when a string is multiplied by a integer the result is that many copies of the string i.e. str * 2 = HelloHello. The operations with strings do slightly different things than operations with numbers. Here are some interactive mode examples to show that some more. >>> "This" + " " + "is" + " joined." 'This is joined.' >>> "Ha, " * 5 'Ha, Ha, Ha, Ha, Ha, ' >>> "Ha, " * 5 + "ha!" 'Ha, Ha, Ha, Ha, Ha, ha!' >>> Here is the list of some string operations: Operation Symbol Example Repetition * "i" * 5 == "iiiii" Concatenation + "Hello, " + "World!" == "Hello, World!" -------------------------------------------------------------------------------- the tutorial page has the code to input then the author says what output he gets and you should also get. im confused at the examples: -------------------------------------------------------------------------------- One more program before I end this chapter: num = input("Type in a Number: ") str = raw_input("Type in a String: ") print "num =", num print "num is a", type(num) print "num * 2 =", num * 2 print "str =", str print "str is a", type(str) print "str * 2 =", str * 2 The output I got was: Type in a Number: 12.34 Type in a String: Hello num = 12.34 num is a <type 'float'> num * 2 = 24.68 str = Hello str is a <type 'string'> str * 2 = HelloHello -------------------------------------------------------------------------------- i dont get the output he says and i dont really understand his explanations. I hope you guys can figure out this post i may have set it up weird. Just to be clear everything inside the dotted lines is straight from the book, not my words.

Share this post


Link to post
Share on other sites
mikeman    2942
Hm. You're not asking any questions here and you're very vague. Instead of copying pages from the book and saying 'I don't understand them,please explain', try to form a question out of it. What exactly is it that you don't you understand?

Share this post


Link to post
Share on other sites
biggjoee5790    229
yea i figured that I didnt make much sense. Ok well the book has a code that you have to input. Then he says what output you should get when you run the program. For most of the ones on this page, my output is different than his. This part in particular has me confused:

-------------------------------------------------------------------------------

One more program before I end this chapter:

num = input("Type in a Number: ")
str = raw_input("Type in a String: ")
print "num =", num
print "num is a", type(num)
print "num * 2 =", num * 2
print "str =", str
print "str is a", type(str)
print "str * 2 =", str * 2

The output I got was:

Type in a Number: 12.34
Type in a String: Hello
num = 12.34
num is a <type 'float'>
num * 2 = 24.68
str = Hello
str is a <type 'string'>
str * 2 = HelloHello
--------------------------------------------------------------------------------

in the input, where it says "type in a number" and "type in a string", am i supposed to write that or actually put a number? Im just confused about this topic in general really. He explains something about "num" and "raw_input" but i just dont get it. My output when I run the program is usually blank or different than his. I hope im being clear and you can help me out. I realize that what Im supposed to be doing is making a program that allows the user to input information, then the program uses their input to make calculations, im just not sure what to do

[Edited by - biggjoee5790 on March 5, 2007 7:25:25 PM]

Share this post


Link to post
Share on other sites
mikeman    2942
When you doubt how things work, look at your reference to see exactly what a function does, what are its arguments, and what does it return.

By the way, that article makes a rather big mistake,specifically this:
str = raw_input("Type in a String: ")

str is a reserved word for Python, and it's used to represent the built-in type 'str'(that is,a string). So you can't really use it as variable's name.

Now,let's look at some more meaningful examples:

>>name=raw_input("Type your name:")

this line,when executed:
1)Prints "Type your name:" to the screen
2)Waits for the user to input a string
3)Whatever the user inputs, gets stored in variable 'name'

>>age=input("Type your age:")

The function 'input' behaves somewhat similar to raw_input, except it doesn't expect a string from the user, but any Python expression. Some examples of what a user can input:

>>2+15-1
returns the integer 16

>>"Hello world"
returns the string "Hello world"

>>foo_var
if foo_var variable actually exists, it will return its value. Otherwise, it will throw an exception.

So you see, basically it's like letting the user writing Python code that gets evaluated on the fly.

I don't really recommend using 'input', because it's a little too powerful, as it accepts any expression. The user can even call Python functions through it, and we don't want that. So, it would be better if we used 'raw_input' to get a string, and then construct the object we want out of that string. For example:

>>age_string=raw_input("Type your age:")
>>age=int(age_string)#convert the string into an integer

or, even shorter:

>>age=int( raw_input("Type your age:") )

[Edited by - mikeman on March 5, 2007 8:32:53 PM]

Share this post


Link to post
Share on other sites
When you call input() and raw_input(), the program will print the argument (in this case, "Type your age: ") before letting the user input their value. It simply lets your prompt your user of what is supposed to be entered during the function call itself.

Share this post


Link to post
Share on other sites
Kylotan    10008
Quote:
Original post by biggjoee5790
Ok so where you say "type your age", or "enter your name", a user replaces that with his or her name or age, then the program will use what they input?


The program asks for input, and the user then provides it. Then the program stores that input for later use.

In the last example you posted, the author showed the number and the string that they typed, plus the output that the program gives. If you type in a different number and a different string, you will see different output, as the program varies the output according to the inputs.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this