python - My unit checking variable isn't reading the input properly, when I put cm in as input the program sends the boolean to the else -
when enter cm ucheck variable program prints string in else condition
ucheck=input(print('unit?')) #<--i enter 'cm' here if ucheck=='nm': wave=wave if ucheck=='cm': #<--it seems skip boolean wave=wave*(10**7) if ucheck=='mm': wave=wave*(10**6) if ucheck=='m': wave=wave*(10**9) else: print('invalid unit! valid units are: nm, mm, cm, m.') #<-- , prints frequency()
your if
statements separate. if first 1 true, you'll still check second one, , third one, , fourth one, , since first 1 true, else
block executed.
change them elif
, code should work:
ucheck = input('unit? ') if ucheck == 'nm': wave = wave elif ucheck == 'cm': ...
also, better way dictionary:
units = { 'nm': 1, 'mm': 10**6, 'cm': 10**7, 'm': 10**9 } unit = input('unit? ') if unit in units: wave *= units[unit] else: print('invalid unit! valid units are: ' + ', '.join(units)) frequency()
Comments
Post a Comment