-2
print(
        """
 __   __     __                         
| |  | |    | |                          
| |  | | ___| | ___ ___  _ __ ___   ___  
| |/\| |/ _ \ |/ __/ _ \| '_ ` _ \ / _ \ 
\  /\  /  __/ | (_| (_) | | | | | |  __/
 \/  \/ \___|_|\___\___/|_| |_| |_|\___| 
"""
.strip())

print("\t\t\t\tto the pHinator 9001")

ph = input("\nPlease enter a pH value:")

if ph == "7":
    print("\nA pH value of 7 is NEUTRAL")

if "0" < ph < "7":
    print("\nA pH value of less than 7 is ACIDIC")

if "14" > ph > "7":
    print("\nA pH value of more than 7 is ALKALINE")

if ph < "0":
        print("\nAn error occurred. Value must be between 0 and 14")

if ph > "14":
        print("\nAn error occurred. Value must be between 0 and 14")

cont = input("\nPress 1 to enter a new value, Press 2 to exit.")

if cont == "1":
    (RESTART)
if cont == "2":
    input("\n\nPress the enter key to exit.")
    exit

This is my first attempt at making a proper python program, and I've looked around for this but couldn't find a useful answer that I knew how to apply to my program, Thanks :)

J.Hogg
  • 1
  • 1

1 Answers1

-1

if I was you I would wrap the entirety of the program into a function. i.e

def func_name():
    print("\t\t\t\tto the pHinator 9001")

    ph = input("\nPlease enter a pH value:")

    if ph == "7":
        print("\nA pH value of 7 is NEUTRAL")

    if "0" < ph < "7":
        print("\nA pH value of less than 7 is ACIDIC")

    if "14" > ph > "7":
        print("\nA pH value of more than 7 is ALKALINE")

    if ph < "0":
            print("\nAn error occurred. Value must be between 0 and 14")

    if ph > "14":
           print("\nAn error occurred. Value must be between 0 and 14")

    cont = input("\nPress 1 to enter a new value, Press 2 to exit.")

    if cont == 1:
        func_name()
    if cont == "2":
        input("\n\nPress the enter key to exit.")
        exit

func_name()

Also make sure you aren't supposed to be comparing strings. Change "0" to 0, etc.

elephant
  • 69
  • 2
  • 9
  • I've done what you suggested, but my program didn't appear, unsure what happened. – J.Hogg Sep 04 '15 at 20:03
  • you must do it recursively. i.e. the if cont == 1 should be nested in the func_name(). Also make sure you are comparing integers not strings. "14" > "7" doesn't make sense since these are both strings. It should be 14 and 7 instead of "14" and "7". The same goes for cont == "1" vs. cont == 1 – elephant Sep 04 '15 at 20:13
  • @J.Hogg Also, make sure you call the function initially. Somewhere outside the indent. I will edit the code above to make it more similar – elephant Sep 04 '15 at 20:14
  • Half fixed, it now runs well, but pressing 1 just ends the program instead of restarting it.(I have fixed the string comparing) – J.Hogg Sep 04 '15 at 20:23
  • are you sure you the function is retriggering when you press 1? it works for me – elephant Sep 05 '15 at 00:34
  • Using recursion like that is _not_ a good design strategy. – PM 2Ring Sep 05 '15 at 11:53
  • @PM2Ring what is wrong with it? – elephant Sep 05 '15 at 17:10
  • 1
    @elephant see the [accepted answer on the duplicate question](http://stackoverflow.com/a/23294659/918959); "Recursion Will Blow Your Stack", as well as guidances on how to do this right. – Antti Haapala -- Слава Україні Sep 06 '15 at 08:30
  • There's also some useful info at [Why does Python have a maximum recursion depth?](http://stackoverflow.com/q/26873627/4014959). Recursion is great when you _need_ it (like when you're working with trees or other recursive data structures), but in Python it's best to avoid it in cases where you can just use simple looping. – PM 2Ring Sep 06 '15 at 08:38