你好,我正在编写一个程序,电脑会随机生成一个风速,风暴潮为一个飓风。到目前为止,它将随机生成这两个东西。但是现在我不知道如何将函数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
此函数有效地打印范围(74157)内的随机数。如果要使用该数字,必须返回一个值:
然后调用函数就可以对结果做它需要做的事情——大概,它会先将结果保存到某个变量中,然后它可能会打印值或计算类别之类的东西。你知道吗
如果要根据某个数字计算类别,最明显的方法如下:
您的代码如下所示:
你可以这样使用:
或者更简单:
不要忘记使用函数的返回值')
相关问题 更多 >
编程相关推荐