8 Replies - 792 Views - Last Post: 19 September 2011 - 06:07 AM Rate Topic: -----

#1 Mwain6190  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 07-June 11

Starting in Python....

Posted 13 September 2011 - 03:28 PM

I am new to the Python language and I seem to be having trouble with the following code. It seems logical correct to me but it is not working properly. When the determinePH function is called, it seems to go straight to the else statement and does not get evaluated. Any suggestions would be appreciated.


#Function gets value from user

def getValue():
    
   input = raw_input("Enter a value: ");
   if (input == -99):
       print ("Program terminating.....");
   else:
       determinePH(input);
        

#Function takes user input as an arguement and determines pH scale.

def determinePH(value):
    
        if (value > 12 and value < 16):
            print ("pH\t", value + "Alkaline");
        elif (value < 12 and value > 7):
            print ("pH\t", value + "Very alkaline");
        elif (value == 7):
            print ("pH\t", value + "Neutral");
        elif (value < 7 and value > 2):
            print ("pH\t", value +  "Acidic");
        elif (value < 2 and value >= 0):
            print ("pH\t", value + "Very acidic");
        else:
           print ("Invalid input! Input range is [0,15]");      
        
        return;
        
#Function defines main
        
def main() :
    print "Valid pH values are [0,15]";
    print "Use -99 to terminate the program";
    print "--------------------------------";
    getValue();
    
    
    
   


#Begin main
    
main();




Is This A Good Question/Topic? 0
  • +

Replies To: Starting in Python....

#2 izthrower  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 233
  • Joined: 11-February 09

Re: Starting in Python....

Posted 13 September 2011 - 03:50 PM

Are you using python 3.1 or python 2.6?
Was This Post Helpful? 0
  • +
  • -

#3 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: Starting in Python....

Posted 13 September 2011 - 04:08 PM

What I think you'll find is that the return value from raw_input is actually going to be a string, not a number. Therefore, you will need to cast the input to an integer or float.
Was This Post Helpful? 1
  • +
  • -

#4 izthrower  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 233
  • Joined: 11-February 09

Re: Starting in Python....

Posted 13 September 2011 - 04:14 PM


def getValue():
    
   x = int(input("Enter a value: "))
   return x
        

#Function takes user input as an arguement and determines pH scale.

def determinePH(value):
    
        if value > 12 and value < 16:
            print ("pH", value,  "Alkaline")
        elif value < 12 and value > 7:
            print ("pH", value , "Very alkaline")
        elif value == 7:
            print ("pH", value , "Neutral")
        elif value < 7 and value > 2:
            print ("pH", value ,  "Acidic")
        elif value < 2 and value >= 0:
            print ("pH", value , "Very acidic")
        else:
            print ("Invalid input! Input range is [0,15]")      
        
        
        
#Function defines main
        
def main() :
    print ("Valid pH values are [0,15]")
    print ("Use -99 to terminate the program")
    print ("--------------------------------")
    y = getValue()
    determinePH(y)
    
    
   


#Begin main
    
main()





should look something like this if you are programing in 3.1. The problems you had were first you dont need the ; its not java in python you dont need the ;. Also you need to make sure you arnt using reserved words. Input is a reserved word and there for you cant use it as a variable name. The int() changes the input in to a integer so you can compare values in the if and elif statment. The final thing is you need to make sure your calling you functions correctly in your main.
Was This Post Helpful? 1
  • +
  • -

#5 Mwain6190  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 07-June 11

Re: Starting in Python....

Posted 13 September 2011 - 06:19 PM

View Postizthrower, on 13 September 2011 - 04:14 PM, said:


def getValue():
    
   x = int(input("Enter a value: "))
   return x
        

#Function takes user input as an arguement and determines pH scale.

def determinePH(value):
    
        if value > 12 and value < 16:
            print ("pH", value,  "Alkaline")
        elif value < 12 and value > 7:
            print ("pH", value , "Very alkaline")
        elif value == 7:
            print ("pH", value , "Neutral")
        elif value < 7 and value > 2:
            print ("pH", value ,  "Acidic")
        elif value < 2 and value >= 0:
            print ("pH", value , "Very acidic")
        else:
            print ("Invalid input! Input range is [0,15]")      
        
        
        
#Function defines main
        
def main() :
    print ("Valid pH values are [0,15]")
    print ("Use -99 to terminate the program")
    print ("--------------------------------")
    y = getValue()
    determinePH(y)
    
    
   


#Begin main
    
main()





should look something like this if you are programing in 3.1. The problems you had were first you dont need the ; its not java in python you dont need the ;. Also you need to make sure you arnt using reserved words. Input is a reserved word and there for you cant use it as a variable name. The int() changes the input in to a integer so you can compare values in the if and elif statment. The final thing is you need to make sure your calling you functions correctly in your main.



I am using version 2.7 - I made the changes to the code:



#The following program reads one integer at a time from the user. If the
# input is within a valid range, the prgram outputs how acidic or basic
# the pH is.


#Function gets value from user

def getValue():
    
   value = int(raw_input("Enter a value: "));
   if (value != 99):
       return value
   else:
       print ("Program terminating.....");
     
        

#Function takes user input as an arguement and determines pH scale.

def determinePH(value):
    
        if (value > 12 and value < 16):
            print ("pH\t", value + "Alkaline");
        elif (value < 12 and value > 7):
            print ("pH\t", value + "Very alkaline");
        elif (value == 7):
            print ("pH\t", value + "Neutral");
        elif (value < 7 and value > 2):
            print ("pH\t", value +  "Acidic");
        elif (value < 2 and value >= 0):
            print ("pH\t", value + "Very acidic");
        else:
           print ("Invalid input! Input range is [0,15]");      
        
        return;
        
#Function defines main
        
def main() :
    print "Valid pH values are [0,15]";
    print "Use -99 to terminate the program";
    print "--------------------------------";
    choice = getValue();
    determinePH(choice);
    
    
   


#Begin main
    
main();






I am getting the following errors:

Traceback (most recent call last):
File "C:\Python27\Lib\SITE-P~1\PYTHON~2\pywin\framework\scriptutils.py", line 325, in RunScript
exec codeObject in __main__.__dict__
File "C:\Users\Desktop\lab01.py", line 57, in <module>
main();
File "C:\Users\Desktop\lab01.py", line 49, in main
determinePH(choice);
File "C:\Users\Desktop\lab01.py", line 28, in determinePH
print ("pH\t", value + "Alkaline");
TypeError: unsupported operand type(s) for +: 'int' and 'str'
Was This Post Helpful? 0
  • +
  • -

#6 izthrower  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 233
  • Joined: 11-February 09

Re: Starting in Python....

Posted 14 September 2011 - 09:36 AM

you cant add the int value you to a string. So if you get replace the + with a , it should work fine. Im assuming that you just want to print (say the value is 3) ph 3 acidic. correct?
Was This Post Helpful? 1
  • +
  • -

#7 Mwain6190  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 07-June 11

Re: Starting in Python....

Posted 14 September 2011 - 10:48 AM

View Postizthrower, on 14 September 2011 - 09:36 AM, said:

you cant add the int value you to a string. So if you get replace the + with a , it should work fine. Im assuming that you just want to print (say the value is 3) ph 3 acidic. correct?



Thanks to everyone for their assistance. Below is the final code. Please let me know if I could have been more efficient.


#The following program reads one integer at a time from the user. If the
# input is within a valid range, the prgram outputs how acidic or basic
# the pH is.


#Function gets value from user

def getValue():
    
   value = int(raw_input("Enter a value: "))
   
   return value
  
#Function takes user input as an arguement and determines pH scale.

def determinePH(choice):
    
        if (choice > 12 and choice < 16):
            print "pH %3d" "\tAlkaline" % choice
        elif (choice <= 12 and choice > 7):
            print "pH %3d" "\tVery alkaline" % choice
        elif (choice == 7):
            print "pH %3d" "\tNeutral" % choice
        elif (choice < 7 and choice > 2):
            print "pH %3d" "\tAcidic" % choice
        elif (choice <= 2 and choice >= 0):
            print "pH %3d" "\tVery acidic" % choice
        else:
           print "Invalid input! Input range is [0,15]"   
        
        return
        
#Function defines main
        
def main() :
    
       print "Valid pH values are [0,15]"
       print "Use -99 to terminate the program"
       print "--------------------------------"
       choice = getValue()
       while (choice != -99):
          determinePH(choice)
          choice = getValue()
       else: print ("Program terminating.....")
    
    
   


#Begin main
    
main();



Was This Post Helpful? 0
  • +
  • -

#8 biblakysads  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 31-May 11

Re: Starting in Python....

Posted 18 September 2011 - 04:10 PM

I got a tricky problem:
I write a python program that builds a path to a file. After building, it starts an other program using
subprocess. The program being started is a kind of tabbed text editor that opens a separate tab
for every textfile being opened but when I use my program for starting, it doesnt create a new tab, instead a new program instance of the editor is created.
I used to make a workaround with os.startfile and os.popen"start test.exe filename.txt" but it didnt work.
I guess I have to use a Windows API but Im not sure about it
Was This Post Helpful? 0
  • +
  • -

#9 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: Starting in Python....

Posted 19 September 2011 - 06:07 AM

More likely you'll need to learn the command-line argument that causes your program to open it in a tab.

Example:
Mozilla Firefox has a setting which determines if new links are opened in a new tab or a new window. On the command line, you can force Firefox to use one or the other:
firefox.exe new-tab http://google.com/

firefox.exe new-window http://google.com/

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1