Support Forums

Full Version: Help please
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all, I'm creating a program that does calculations for converting alertpay to paypal and stuff, but I'm having an error that I can't seem to fix. It wasn't working on 2.7 so now I'm on 2.6 and it still doesn't work -__-

Code:
#!/usr/bin/python
import time
initial = raw_input('Enter exchange amount here: ')
exchangefrom = raw_input('Is the exchange starting from alertpay or paypal?: ')
exchangeto = raw_input('Is the exchange to alertpay or paypal?: ')

if initial < 10:
    print 'No exchanges allowed under $10'
    time.sleep(100)
    
else initial > 150:
    a=initial-initial*.025 - .25
    b=a-.5
    c=b-initial*.065
    d=c-c*.029-.3
    print(d)
    time.sleep(100)

It says my syntax error is the first initial after else.
Give me a second and I'll try to give you an answer.

P.S. I think that 2.7 and 2.6 have the same syntax for the most part.
The only thing that would be a problem would be 2 to 3.

I think the problem is probably this part.
Code:
else initial > 150:
    a=initial-initial*.025 - .25
    b=a-.5
    c=b-initial*.065
    d=c-c*.029-.3
    print(d)
    time.sleep(100)

Replace that part with this.
Code:
else:
    a=initial-initial*.025 - .25
    b=a-.5
    c=b-initial*.065
    d=c-c*.029-.3
    print(d)
    time.sleep(100)
Thanks for the response. It did work after I removed the part you suggested, but I need it to run that part if the user input is greater than 150. Is there any other way to make it work?
Sorry I didn't see that is was only if it's greater than 150.
Give me a second.

Give this a try.
Code:
import time
initial = int(raw_input('Enter exchange amount here: '))
exchangefrom = raw_input('Is the exchange starting from alertpay or paypal?: ').lower()
exchangeto = raw_input('Is the exchange to alertpay or paypal?: ').lower()

if initial < 10:
    print 'No exchanges allowed under $10'
    time.sleep(10)
    
elif initial > 150:
    a=initial-initial*.025 - .25
    b=a-.5
    c=b-initial*.065
    d=c-c*.029-.3
    print(d)
    time.sleep(10)

Notice that for the first variable (initial) I put int( .... ) around it.
The reason being is that raw_input stores it as a string. You have to convert it to an integer for it to work.

Your sleep was set to 100 seconds. I lowered it to 10.

I also added .lower() to the end (With the period) so that it will convert the string to lower case. If you check the string to notice which one is converting to which it would be easier if the string was lower case so you don't have to account for every variation of case.

If you want to have multiple if statements you use
Code:
elif

Which is known as else if in some other languages.
This is weird, I added an elif with another comparison and that one worked....

Code:
#!/usr/bin/python
import time
initial = float(raw_input('Enter exchange amount here: '))
exchangefrom = raw_input('Is the exchange starting from alertpay or paypal?: ')
exchangeto = raw_input('Is the exchange to alertpay or paypal?: ')

if initial < 10:
        print 'No exchanges allowed under $10'
        time.sleep(100)
        
elif initial >= 10 and initial < 25:
        a=initial-initial*.025 - .25
        b=a-.5
        c=b-initial*.025
        d=c-c*.029-.3
        print(d)
        time.sleep(100)
        
else:
        a=initial-initial*.025 - .25
        b=a-.5
        c=b-initial*.065
        d=c-c*.029-.3
        print(d)
        time.sleep(100)
I just updated when you posted and explained.

Ignore the int part.
I just remembered that you were using money conversion.

It would be the same idea, except with float instead of int.

int is integers, so only whole numbers.
float would include decimal values.
(08-16-2010, 07:27 PM)Road Kamelot Wrote: [ -> ]I just updated when you posted and explained.

Ignore the int part.
I just remembered that you were using money conversion.

It would be the same idea, except with float instead of int.

int is integers, so only whole numbers.
float would include decimal values.

Ok I was about to ask if I should use int or float.
Thanks a ton for the help, +1 to you
Just a few questions..
1) Do I need to finish off with an else statement or can I just use a bunch of the elif's instead?
2) Can I use something like raw_input('Press<enter>') instead of time?
3) Do you have any idea why it worked with elif and not else?
4) How come you removed #!/usr/bin/python
1) Finishing off with an elif should work if you wanted to set a value.
Else is used because it encompasses everything that you didn't want to clarify.
So let's say you only checked.

100 < x < 150 (Greater than 100 less than 150)
and
180 < x < 250 (Greater than 180 less than 250)

Then you finished off with an else it would include less than 100 , less than 180 greater than 150, and also greater than 250.

So an else would be better in that situation.

2) You could if it's a smaller app, but once you get into coding large projects it would seem tacky.

3) Because when you use an else you can't set what needs to be true.
Else statements are used when you want something to happen for what you didn't make a statement for.

4) Because that's used to set the path. There isn't a need to set something that is already set.
I haven't gotten far into Python, but I think that would only be needed if you're path was set for Python 2.6 and then, for example, you wanted to use Python 3.1 for that one app.

Also some distros use scripts that are dependent on a certain version of Python so you have to use that line to set the path for the version of Python you wish to use.
Ohh I see now, thank you so much for your help Road Kamelot, lookin forward to seein you around
Thanks. Smile
I'm not that good at Python, but I'm constantly learning.