yet another quick question...

I wrote this at the end of a script

y = 1
n = 0
i = input("Again? Y/N: ")
if i is 0:
    print ("Good bye!")
    exit   
if i is 1:
    execfile ("program2.py")

now of course if i is 0, they won't be able to see the "good bye!" because it will only last for like half a second...

is there any command to like say "wait 1 second"?

as long as quick command questions go, is it possible to write exponents? because like if you write 3^2 it won't get you 9, it will get you 1 for some reason...

Recommended Answers

All 11 Replies

import time
time.sleep(5.5) #sleep for 5.5 seconds
3**2 == 9

thanks, perfect!

i'll try to stop posting stupid questions

Just a couple more comments:
You should consider using True and False instead of using this:

y = 1
n = 0

You're not using them anyway, so they can be removed.

Also, you shouldn't use execfile like this. That functionality should be handled with a loop:

i = True
while i == True:
    #do something
    #ask if they want to continue
print('Good bye!')

This would also eliminate the need for sleep, which will only serve to annoy users.

yeah i know y=1 and n=0 shouldn't be necessary...
but when i take y=1 out, and change it to "if i is y" then it says (when running module out of idle)

Traceback (most recent call last):
File "C:\Users\Max\Desktop\program2.py", line 38, in <module>
    i = input("Again? Y/N: ")
  File "<string>", line 1, in <module>
NameError: name 'y' is not defined

so i put y=1 as a quick fix. i took out n=0 and changed it to "else".

i'm definitely going to have to learn how to make loops (i do realize why that's better than looping the whole program) but how would it eliminate the need for sleep? it would automatically exit anyways immediately after printing (good bye!) i just wanted it to stop for a second so they can see the "good bye!"... works great.

y=1
if i is 1:
    execfile ("program2.py")
else:
    print ("Good bye!")
    import time
    time.sleep(1)

if you want, you can see it in context (file below)
of course i don't actually need it for anything, just playing around making very simple programs

Instead of sleeping for the user to see the end message you can place an input at the end of your program so that the user has to hit <Enter> to quit like this:

# This is the last line of code
raw_input( "Press <Enter> to exit..." )

oh, alright thanks...that was actually the first way i did it, but i did not like it because it exited due to syntax not the command exit... although i guess you could say...

by the way please ignore the file above, its embarrasingly simple... i did modify it to use a loop, which works much better (for one thing, the title doesn't repeat)...and i took out those silly loops when you get a question wrong, changed it to simply calculate the score at the end.

I know this is a solved thread, but I thought I should clearify the second question about why 3^2 is 1 instead of 9. The exponential operator in python is **, as correctly given above. The ^ operator, however, is XOR (exclusive or). This operator works in the way that it's true if only one, and exactly one of the arguments is true. Example: 3^2 = 1 Why? 3 is not 2, therefore the expression evaluates to 1. 3^3 = 0 Here we have the same argument on each side of the operator - it evaluates to 0.

http://en.wikipedia.org/wiki/XOR_gate gives a better explanation of how it works.

oh ok ...interesting. does it do any other gates?

actually nevermind, i'll look it up myself. maybe i can use it to make an interesting program.

I know this is a solved thread, but I thought I should clearify the second question about why 3^2 is 1 instead of 9. The exponential operator in python is **, as correctly given above. The ^ operator, however, is XOR (exclusive or). This operator works in the way that it's true if only one, and exactly one of the arguments is true. Example: 3^2 = 1 Why? 3 is not 2, therefore the expression evaluates to 1. 3^3 = 0 Here we have the same argument on each side of the operator - it evaluates to 0.

http://en.wikipedia.org/wiki/XOR_gate gives a better explanation of how it works.

Excuse me if I've misunderstood you but this isn't correct. You have the right answer for those two expressions but the result isn't always 0 or 1.

You have to convert the numbers to their binary representations. If either (but *not* both) of the corresponding bits of the two numbers is 1, the result for that bit is 1. Otherwise, it's 0. You then have a result that is as many bits as the largest number, which can be converted back to decimal. So the results are not just 0 or 1. Of course, Python does all of this for you.

So 3 is 11 and 2 is 10 in binary.
11
10
01

Therefore 3^2 is 1.


Another example:
250 in binary is 11111010
5 in binary is 101
11111010
00000101
11111111

11111111b in decimal is 255.

Therefore 250^5 is 255 because in each bit one of the numbers (but not both) has a 1 in that place.

For completion's sake:
3d = 11b
11
11
00

3^3 == 0

One last one:
77d = 1001101b
74d = 1001010b

1001101
1001010
0000111

111b = 7d

77^74 = 7

commented: good explanation :) +2

My bad, you're quite right. Forgot for a moment that python doesn't treat numbers as boolean values (true if > 0, else false). In afterthought - that's a really stupid assumption to make :$

My point was anyway to say that it was the XOR operator, and not the exponential operator which it's easily mistaken for. Thanks for the clearup :)

Forgot for a moment that python doesn't treat numbers as boolean values (true if > 0, else false). In afterthought - that's a really stupid assumption to make :$

It does, just not in this case. ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.