豆嘉木 发表于 2022-3-17 19:37:13

python这里为什么报错

运行时系统有报错,说是第一个while错了,不知道是哪里错了

import random

while :
    goon = input("是否继续实验? \n继续扣'1' \n结束扣'0'")
    if goon = 0:
      break
    a = 0
    b = 0
    c = 0
    d = 0
    temp = input('需要做多少次实验')
    turn = int(temp)
    while turn > 0:
      result = random.randint(1,4)
      if result = 1:
            a = a + 1
      else:
            if result = 2:
                b = b + 1
            else:
                if result = 3:
                  c = c + 1
                else:
                  d = d + 1
      turn = turn - 1
    e = 1/a
    f = 1/b
    g = 1/c
    h = 1/d
    print("抽到'1'的概率为",e)
    print("抽到'2'的概率为",f)
    print("抽到'3'的概率为",g)
    print("抽到'4'的概率为",h)
    print("本轮实验结束")
   
      
      
      

豆嘉木 发表于 2022-3-17 19:38:25

其他地方还没完善,但这个while属实给我整懵了

isdkz 发表于 2022-3-17 19:42:14

本帖最后由 isdkz 于 2022-3-17 19:44 编辑

豆嘉木 发表于 2022-3-17 19:38
其他地方还没完善,但这个while属实给我整懵了

while 后面得跟上表达式,你可以改成 while 1: 就是死循环了,

不是说死循环就不需要加 表达式 了

ckblt 发表于 2022-3-17 19:44:27

isdkz 发表于 2022-3-17 19:42
while 后面得跟上表达式,你可以改成 while 1: 就是死循环了,

不是说死循环就不需要加 表达式 了

while True: 更好

isdkz 发表于 2022-3-17 19:49:24

本帖最后由 isdkz 于 2022-3-17 19:56 编辑

ckblt 发表于 2022-3-17 19:44
while True: 更好

为什么呀?

ckblt 发表于 2022-3-17 19:55:00

isdkz 发表于 2022-3-17 19:49
为什么?

因为True是bool类型,而1是int类型。
你这样也不错,取决于个人习惯

isdkz 发表于 2022-3-17 19:55:55

ckblt 发表于 2022-3-17 19:55
因为True是bool类型,而1是int类型。
你这样也不错,取决于个人习惯

好吧{:5_96:}

豆嘉木 发表于 2022-3-17 20:17:10

ckblt 发表于 2022-3-17 19:44
while True: 更好

哦~,多谢大佬
页: [1]
查看完整版本: python这里为什么报错