Python如果用elif程序来计算光的波长

时间:2015-09-14 16:24:37

标签: python python-2.7 python-3.x

我正在进行一项Python编程,它要求用户提供波长值,并打印出相应电磁波谱部分的描述,如下表所示。

| Type          | Wavelength             | Frequency                |
|---------------+------------------------+--------------------------|
| Radio Waves   | 10^–3 to 10^–1         | 3 × 10^9 to 3 × 10^11    |
| Microwaves    | 10^–3 to 10^–1         | 3 × 10^9 to 3 × 10^11    |
| Infrared      | 7 × 10^–7 to 10^–3     | 3 × 10^11 to 4 × 10^14   |
| Visible Light | 4 × 10^–7 to 7 × 10^–7 | 4 × 10^14 to 7.5 × 10^14 |
| Ultraviolet   | 10^–8 to 4 × 10^–7     | 7.5 × 10^14 to 3 × 10^16 |
| X-Rays        | 10^–11 to 10^–8        | 3 × 10^16 to 3 × 10^19   |
| Gamma Rays    | < 10^–11               | > 3 × 10^19              |

现在我尝试使用多个if ifif语句来获得结果,但不知何故我得到缩进错误,我知道我在整个编码中也有些不正确。我是一名初学程序员,他正在学习Python作为一种起始语言(仅在一周前开始)。这是我一直在研究的代码。有人可以帮我提供代码吗?

wavelength = int(input('Please enter the wavelength...'))

if wavelength > 1E-1:
    frequency < 3E9, Wtype = Radio Waves

elif 1E-3 <= wavelength <= 1E-1:
    3E9 <= frequency <= 3E11, Wtype = Microwaves


elif 7E-7 <= wavelength <= 1E-1:
    3E11 <= frequency <= 4E14, Wtype = Infrared

elif 4E-7 <= wavelength <= 7E-7:
    4E14 <= frequency <= 7.5E14, Wtype = Visible Light

elif 1E-8 <= wavelength <= 4E-7:
    7.5E14 <= frequency <= 3E16, Wtype = Ultraviolet

elif 1E-11 <= wavelength <= 1E-8:
    3E16 <= frequency <= 3E19, Wtype = X-Rays



elif  wavelength <= 1E-11:
    frequency > 3E19, Wtype = Gamma Rays


print('Your wavelength is{0}'.format(Wtype))

更新:

我在代码上即兴创作了一些。

本,谢谢你的意见。我根据给定的要求即兴创作了代码。这里是。无论我输入什么输入,它都会提供&#34; Gamma Rays&#34;作为输出。

while True:
    wavelength = float(input('Please enter the wavelength...'))



    if 1E-3 <= wavelength <= 1E-1:
        Wtype = 'Radio Waves'

    elif 1E-3 <= wavelength <= 1E-1:
        Wtype = 'Microwaves'


    elif 7E-7 <= wavelength <= 1E-1:
        Wtype = 'Infrared'

    elif 4E-7 <= wavelength <= 7E-7:
        Wtype = 'Visible Light'

    elif 1E-8 <= wavelength <= 4E-7:
        Wtype = 'Ultraviolet'

    elif 1E-11 <= wavelength <= 1E-8:
        Wtype = 'X-Rays'

    elif  wavelength >= 1E-11:
        Wtype = 'Gamma Rays'


    print('Your wavelength is {0}'.format(Wtype))

1 个答案:

答案 0 :(得分:1)

您的代码存在一些问题:

  1. 3E9 <= frequency <= 3E11, Wtype = Microwaves;使用;,而非,分隔行
  2. Wtype = Microwaves;你从未定义Microwaves,所以这也会引发错误。您必须使Microwaves等于'Microwaves',或者更简单地将Wtype = 'Microwaves'(字符串必须包含在" "' '中)
  3. 我将代码修改为它的工作原理:

    while True:
    wavelength = float(input('Please enter the wavelength: '))
    
    
    
    if 1E-3 <= wavelength <= 1E-1:
        Wtype = 'Radio Waves'
    
    elif 1E-3 <= wavelength <= 1E-1:
        Wtype = 'Microwaves'
    
    
    elif 7E-7 <= wavelength <= 1E-1:
        Wtype = 'Infrared'
    
    elif 4E-7 <= wavelength <= 7E-7:
        Wtype = 'Visible Light'
    
    elif 1E-8 <= wavelength <= 4E-7:
        Wtype = 'Ultraviolet'
    
    elif 1E-11 <= wavelength <= 1E-8:
        Wtype = 'X-Rays'
    
    elif  wavelength >= 1E-11:
        Wtype = 'Gamma Rays'
    
    
    print('Your wavelength is {0}'.format(Wtype))
    

    这是一个测试运行:

    >>> Please enter the wavelength: 0.8798798789                                                                                                                     
    Your wavelength is Gamma Rays                                                                                                                                 
    >>> Please enter the wavelength: 0.0000000005                                                                                                                     
    Your wavelength is X-Rays                                                                                                                                     
    >>> Please enter the wavelength: 1E-8                                                                                                                             
    Your wavelength is Ultraviolet                                                                                                                                
    >>> Please enter the wavelength: 1E-11                                                                                                                            
    Your wavelength is X-Rays                                                                                                                                     
    >>> Please enter the wavelength: .00000300000                                                                                                                     
    Your wavelength is Infrared                                                                                                                                   
    >>> Please enter the wavelength: 3E-7                                                                                                                             
    Your wavelength is Ultraviolet                                                                                                                                
    >>> Please enter the wavelength: 4E-7                                                                                                                             
    Your wavelength is Visible Light