如何根据Python中飓风的风速为其指定一个类别

2024-09-29 17:20:52 发布

您现在位置:Python中文网/ 问答频道 /正文

你好,我正在编写一个程序,电脑会随机生成一个风速,风暴潮为一个飓风。到目前为止,它将随机生成这两个东西。但是现在我不知道如何将函数wind中的可变风速回调到函数category中,以使用随机生成的风速来定义它是什么类别。但当我运行程序时,它会打印风速和风暴潮,但当它打印类别时,它不会打印与风速相关的内容。这是我的密码。你知道吗

import os
import sys
import random
import math 
import time

def wind():
    windspeed = randrange(74, 157)
    print "The Hurricanes wind speed is"
    time.sleep(2)
    print windspeed

def stormsurge():
    surgeamount = randrange(3, 19)
    print "The Hurricanes storm storm surge is"
    time.sleep(2)
    print surgeamount

def category():
    categorys = [1, 2, 3, 4, 5]
    for windspeed in range(74, 95):
        print categorys[0]
   else:
       for windspeed in range(96, 110):
           print categorys[1]

wind()
stormsurge()
category()

当我运行它的时候,我得到了。 After running it


Tags: the函数import程序timedef类别wind
2条回答
def wind():
    windspeed = randrange(74, 157)
    print "The Hurricanes wind speed is"
    time.sleep(2)
    print windspeed

此函数有效地打印范围(74157)内的随机数。如果要使用该数字,必须返回一个值:

def wind():
    return randrange(74, 157)

然后调用函数就可以对结果做它需要做的事情——大概,它会先将结果保存到某个变量中,然后它可能会打印值或计算类别之类的东西。你知道吗

如果要根据某个数字计算类别,最明显的方法如下:

def category(windspeed):
  # based on http://www.nhc.noaa.gov/aboutsshws.php
  if windspeed < 74:
    return 0 
  elif 75 <= windspeed < 96:
    return 1 
  if 96 <= windspeed < 111:
    return 2
  elif 111 <= windspeed < 120:
    return 3
  elif 130 <= windspeed < 157:
    return 4
  else: 
    return 5

您的代码如下所示:

windspeed = wind()
hurricane_category = category(windspeed)
print "The windspeed is %d and the category is %d" % (
  windspeed, hurricane_category)

你可以这样使用:

import os
import sys
import random
import math 

def wind():
   windspeed = random.randrange(74, 157)
   print "The Hurricanes wind speed is", windspeed
   return windspeed

def stormsurge():
   surgeamount = random.randrange(3, 19)
   print "The Hurricanes storm storm surge is", surgeamount
   return surgeamount

def category(windspeed, surgeamount):
   categorys = [1, 2, 3, 4, 5]
   for _windspeed in range(74, 95):
         if _windspeed == windspeed:
            print "Storm category is", categorys[0]
            break
   else:
      for _windspeed in range(96, 157):
         if _windspeed == windspeed:
            print "Storm category is", categorys[1]

windspeed = wind()
surgeamount = stormsurge()
category(windspeed, surgeamount)

或者更简单:

import os
import sys
import random
import math 

def wind():
   windspeed = random.randrange(74, 157)
   print "The Hurricanes wind speed is", windspeed
   return windspeed

def stormsurge():
   surgeamount = random.randrange(3, 19)
   print "The Hurricanes storm storm surge is", surgeamount
   return surgeamount

def category(windspeed, surgeamount):
   categorys = [1, 2, 3, 4, 5]
   if windspeed in range(74, 95):
      print "Storm category is", categorys[0]
   else:
      print "Storm category is", categorys[1]

windspeed = wind()
surgeamount = stormsurge()
category(windspeed, surgeamount)

不要忘记使用函数的返回值')

相关问题 更多 >

    热门问题