#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();
Starting in Python....
Page 1 of 18 Replies - 583 Views - Last Post: 19 September 2011 - 06:07 AM
#1
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.
Replies To: Starting in Python....
#2
Re: Starting in Python....
Posted 13 September 2011 - 03:50 PM
Are you using python 3.1 or python 2.6?
#3
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.
#4
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.
#5
Re: Starting in Python....
Posted 13 September 2011 - 06:19 PM
izthrower, 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'
#6
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?
#7
Re: Starting in Python....
Posted 14 September 2011 - 10:48 AM
izthrower, 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();
#8
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
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
#9
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:
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/
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote





|