Click to See Complete Forum and Search --> : (probably a dumb) python question
Ludootje
12-25-2001, 04:42 PM
When I have in python:
something = raw_input('enter something ')
print "blablabla is",something"."
i will see:
blablabla is something .
there is space between something and the point. how do i change that?
also, is it possible to use timers? so for example:
blah = raw_input('blabla')
timer = wait_10_second_before_going_to_the_next_linux
print blah
vee-eye
12-25-2001, 09:39 PM
I'm not sure about the timer, but the print statement separates its arguments with spaces. To avoid spaces, only give the print statement one argument. You can do this by concatenating strings:
your_name = raw_input("Como te llamas? ")
print "Your name is" + your_name
Since your_name is a string, adding the string "Your name is" with your_name will concatenate (or combine) the strings to form this:
Your name isBob
With Bob being the user input. There is a problem in that you cannot concatenate a string and a number in python:
"Your lucky number is" + 38
Will give you an error. You must convert the number to a string with the built in str() function, and then concatenate the two strings:
"Your lucky number is" + str(38)
However, raw_input() will return a string even if the user theoretically enters a number. So:
number = raw_input("Enter your favorite number: ")
print "The coolest number in the world is" + number
This will print:
The coolest number in the world is14
If the user entered 14.
Umm... yeah. :p
inkedmn
12-26-2001, 04:32 PM
also, if you want to delay the next action of the script, you can use the sleep() function (from the time module, i think).
it works something like this:
print "Now Loading..."
sleep(5)
print "Finished Loading."
or something like that. this will cause the script to pause for 5 seconds before continuing. it's pretty cool, but it mostly just creates a dramatic effect. :)
Strike
12-26-2001, 04:37 PM
Originally posted by inkedmn:
<STRONG>also, if you want to delay the next action of the script, you can use the sleep() function (from the time module, i think).
it works something like this:
print "Now Loading..."
sleep(5)
print "Finished Loading."
or something like that. this will cause the script to pause for 5 seconds before continuing. it's pretty cool, but it mostly just creates a dramatic effect. :)</STRONG>
But in order to use sleep() instead of time.sleep(), you would have to do:
from time import sleep
inkedmn
12-26-2001, 04:40 PM
Originally posted by Strike:
<STRONG>But in order to use sleep() instead of time.sleep(), you would have to do:
from time import sleep
</STRONG>
i should've included that, my mistake :)
Ludootje
12-26-2001, 04:48 PM
Thanks guys!
some other questions:
- can i use colors? eg: print "something <font color="red">something else</font>"
well, you get the point ;)
- and i was wondering, can i use python graphically? so instead of printing something in a terminal, it would popup a window.. or do i need another language for that?
- bradmont made an lno list once in python, how can i integrate python in html?
Strike
12-26-2001, 05:01 PM
Originally posted by Ludootje:
<STRONG>Thanks guys!
some other questions:
- can i use colors? eg: print "something <font color="red">something else</font>"
well, you get the point ;)
- and i was wondering, can i use python graphically? so instead of printing something in a terminal, it would popup a window.. or do i need another language for that?
- bradmont made an lno list once in python, how can i integrate python in html?</STRONG>
1. I don't know - it's never seemed that important to me.
2. Yes, just pick one of the GUI toolkits and learn the python bindings for it (GTK, QT, Tcl/Tk, ... all these and more have bindings for Python), or you can learn something like PyGame (http://www.pygame.org), an SDL wrapper.
3. You need to learn CGI for that, basically
1. Hmm.. TLD's always talking about terminal escape codes for stuff like that. Maybe he can help with that.
<shameless>
2. There's a python module, Tkinter, which lets you easily get into GUI stuff ('specially if you've done it before). There isn't lots of docs, but there are some examples and a tutorial scattered around for it. (at least one example of python with tkinter at codeexamples.org)
3. Ben Briggs posted a big example of using python for CGI at codeexamples.org some time ago; covering most of the bases. All you really need is a basic understand of python and a basic understanding of CGI, neither of which are too hard to attain. One thing though, I wouldn't say you integrate python (or perl or c++, like bradmont probably did) into HTML. What you do is write a script, and when a browser tried to access that script, the server instead executes the script. The script then outputs text which gets sent to the browser, usually interpreted as html. This is different from say php or javascript which can actually be contained within html files. (i think). We have examples of all that stuff at codeexamples.org, too. :) god I'm shameless.
</shameless>
recluse.
12-29-2001, 08:35 PM
No you fool!!!! There are no dumb Python questions there are only 1337 Python questions. You see? When you ask a question a 1337 guru will walk by and answer it and then I become more knowledgeable with you. (Knowledge is power)
Benny B
12-29-2001, 09:35 PM
In regards to your question about colourising the console output - yup its possible, if you head over to codeexamples.org there is a python example by Ben Briggs showing you exactly what you need....
Was that a good plug kmj? :D
Gnu/Vince
12-29-2001, 10:45 PM
By the way kmj, I submitted two examples, one in Ruby, one in Python.
Originally posted by Benny B:
<STRONG>In regards to your question about colourising the console output - yup its possible, if you head over to codeexamples.org there is a python example by Ben Briggs showing you exactly what you need....
Was that a good plug kmj? :D</STRONG>
Word. Maybe that site is actually useful. :)
Originally posted by Gnu/Vince:
<STRONG>By the way kmj, I submitted two examples, one in Ruby, one in Python.</STRONG>
Thanks; they've been added. Btw, the python one has already been helpful; I didn't know of the 'global' keyword and had been wondering about the scope issues with local and global variables in a function, lately. That clears things up a bit for me. :D
jemfinch
12-30-2001, 03:34 PM
Originally posted by kmj:
I didn't know of the 'global' keyword and had been wondering about the scope issues with local and global variables in a function, lately. That clears things up a bit for me. :D
2.2 has been released and uses nested scopes by default; you may want to read up on the changes that entails.
Jeremy