Little solver program with loop and decision tree, why does not work?

161 views Asked by At

I would be really grateful if you could help me solving out what is wrong in this code.

  1. The while loops, are not working
  2. When the program ask for the calculation it stops

The overall idea is to make this small program help me during calculation, and maybe if possible, add a visual interface.

def cycle():
while True:
    newcalc = input('Do you want to make a new calculation? \n \t (Y/N)')
    if newcalc.lower() in ["no","n"]:
        newcalc = "no"
        break
    if newcalc.lower() in ["yes","y"]:
        newcalc = "yes"
    else:
        print('This is not the right answer')

def counting_M():
  while True:
    concersion_1 = input('Do you want to convert from M to uM? (Y/N)')
    if concersion_1.lower() in ["y", "yes"]:
       print('This is the result of the conversion {} uM',   str(data/1000)).format(data)
    mw = input('What is the Molecular Weight of your compound, please in g/M?')
    print('Your coumpound weights ug/ml', str((data) / (mw / 1000))).format(data)
    if mw in float:
        print('The data you have entered is not numeric')
        break
data = input('Please, provide the absolute amount of compound you have, without units \n \t')
units = input('Please, indicate the measure unit you have \n  A)M, B)mM, C)uM '
          '\n 1A)g/liter, 2A)mg/ml, 3A)ug/ml \n \t')
if units.lower() == "a" :
 print('Ok so you have M')
 counting_M()
 cycle()   

Thank you

1

There are 1 answers

1
deepbrook On BEST ANSWER

A few things you're doing wrong:

  • def cycle() doesn't return newcalc, rendering its assignement useless, since it isn't visible anywhere outside of the function cycle.
  • It appears that you are assigning data twice, while also not converting from string to float, which crashes your print commands, whenever you return a arithmetic operation (i.e., str((data) / (mw / 1000))).
  • The line if mw in float does not check if mw is a float. Do this by using try/except

example:

try:
    float('0.001')
except ValueError:  # occurs when the string cannot be converted to a  valid float)
    print('Not a valid float string!')

Although I'm not clear on what exactly you're trying to achieve, this code should do all the things you're attempting in your example.

def cycle():
    choice = None
    while not choice:
        newcalc = input('Do you want to make a new calculation? \n \t (Y/N)')
        if newcalc.lower() in ["no", "n"]:
            choice = "no"

        if newcalc.lower() in ["yes", "y"]:
            choice = "yes"
        else:
            print('This is not the right answer')
            continue
    return choice

def counting_M(data):
    while True:
        concersion_1 = input('Do you want to convert from M to uM? (Y/N)')
        if concersion_1.lower() in ["y", "yes"]:
            print('This is the result of the conversion {} uM'.format(str(data/1000)))
        else:
            print('Not converting')
        while True:
            mw = input('What is the Molecular Weight of your compound, please in g/M?')
            try:
                mw = float(mw)
                break
            except ValueError:
                print("Not a valid float!")

        print('Your coumpound weights ug/ml', str((data) / (mw / 1000)))

if __name__ =='__main__':
    while True:
        data = input('Please, provide the absolute amount of compound you have, without units \n \t')
        try:
            data = float(data)
            break
        except ValueError:
            print("Not a valid float value!")

    counting_M(data)
    choice = cycle()
    print(choice)