0

when I enter cm into the uCheck variable the program prints the string in the else condition

uCheck=input(print('Unit?')) #<--I enter 'cm' here
if uCheck=='nm':
    Wave=Wave
if uCheck=='cm': #<--it seems to skip this 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.') #<-- and prints this
    Frequency()
mgilson
  • 300,191
  • 65
  • 633
  • 696
user2446048
  • 15
  • 1
  • 5
  • you can get rid of `print` -- `input('Unit?')` should work just fine ... [reference](http://docs.python.org/3/library/functions.html#input) – mgilson Oct 07 '13 at 02:12
  • Whatever happened to the micro metre? I know mu isn't on most keyboards but um is often accepted. – Steve Barnes Oct 07 '13 at 02:22

1 Answers1

1

Your if statements are separate. Even if the first one is true, you'll still check the second one, and the third one, and the fourth one, and since only the first one was true, the else block will be executed.

Change them to elif and your code should work:

uCheck = input('Unit? ')

if uCheck == 'nm':
    Wave = Wave
elif uCheck == 'cm':
    ...

Also, a better way to do this would be with a 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()
Blender
  • 289,723
  • 53
  • 439
  • 496