奥科儿 发表于 2021-7-24 12:38:49

新手写了个分支,结果无法运行,求教该怎么改呀

它提示NameError: name 'c' is not defined


import random
n = 49
a = random.randint(1,n)
a -= 1
b = a % 4

if b == 0:
    c == 9
   
else:
    c == 5
print(c)

n = n - c
a = random.randint(1,n)
a -= 1
b = a % 4
if b == 1:
    c == 4
elif b == 2:
    c == 4
else:
    c == 8
print(c)

n = n - c
a = random.randint(c)
a -= 1
b = a % 4
if b == 1:
    c == 4
elif b == 2:
    c == 4
else:
    c == 8
print(c)

n = n - c
print(n / 4)

逃兵 发表于 2021-7-24 12:46:47

一个=是赋值
两个==是判断是否相同
另外random.randint()需要传入两个参数
参考代码
import random
n = 49
a = random.randint(1,n)
a -= 1
b = a % 4

if b == 0:
    c = 9
   
else:
    c = 5
print(c)

n = n - c
a = random.randint(1,n)
a -= 1
b = a % 4
if b == 1:
    c = 4
elif b == 2:
    c = 4
else:
    c = 8
print(c)

n = n - c
a = random.randint(1,c)
a -= 1
b = a % 4
if b == 1:
    c = 4
elif b == 2:
    c = 4
else:
    c = 8
print(c)

n = n - c
print(n / 4)

奥科儿 发表于 2021-7-24 13:03:55

逃兵 发表于 2021-7-24 12:46
一个=是赋值
两个==是判断是否相同
另外random.randint()需要传入两个参数


十分感谢!!!
页: [1]
查看完整版本: 新手写了个分支,结果无法运行,求教该怎么改呀