首页 新闻 会员 周边

新手,有几道问题想问一下

0
悬赏园豆:80 [待解决问题]

试题1:计算1-1/2+1/3-1/4…+1/99-1/100。

import math
def test():
for i in range(1,100):
if i % 2 == 0:
countbeyond = math.pow(1.0+1/i+1)
else:
countfront = math.pow(1.0-1/i+1)
return countfront+countbeyond

试题2:编程模拟一个计算机扔骰子,每次出现的骰子点数用随机函数产生(1,2,3,4,5,6),投掷100次骰子,试分别统计各点数出现的次数。

import random
count = []
j = random.randint(1,6)
for n in range (1,100):
count.append(j)
if j == 1:
print("骰子1掷了{}次".format(count[j]))
if j == 2:
print("骰子2掷了{}次".format(count[j]))
if j == 3:
print("骰子3掷了{}次".format(count[j]))
if j == 4:
print("骰子4掷了{}次".format(count[j]))
if j == 5:
print("骰子5掷了{}次".format(count[j]))
if j == 6:
print("骰子6掷了{}次".format(count[j]))

试题3:用随机数生成一个3*3矩阵,然后求对角线元素之和。

import random
import math
ls = list(range(100))
shuffle(ls)
for i in range (1,9):
if i % 3 == 0:
print("\n")
print(ls)
ls = []
def func(a,b):
ls = []
ls.append(b)
return a+b

请问一下,这几个分别有哪些错误?

盛大之页的主页 盛大之页 | 初学一级 | 园豆:122
提问于:2019-05-17 11:35
< >
分享
所有回答(1)
0

1.所有格式化都有问题;

2.math.pow 是两个参数

import math
def test():
    for i in range(1,100):
        if i % 2 == 0:
         countbeyond = math.pow(1.0+1/i+1,1)
        else:
         countfront = math.pow(1.0-1/i+11,3)
    return countfront+countbeyond
print(test())

试题2:

1. count[j] 超过下标

试题3:

1.  shuffle(ls) 未定义

悟行 | 园豆:12559 (专家六级) | 2019-05-17 16:54
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册